diff --git a/src/items.rs b/src/items.rs index 55a4c1b6165..e9d8b7b204d 100644 --- a/src/items.rs +++ b/src/items.rs @@ -165,6 +165,7 @@ enum BodyElement<'a> { } /// Represents a fn's signature. +#[derive(Debug)] pub(crate) struct FnSig<'a> { decl: &'a ast::FnDecl, generics: &'a ast::Generics, @@ -323,6 +324,7 @@ impl<'a> FmtVisitor<'a> { ident: Ident, sig: &ast::FnSig, generics: &ast::Generics, + vis: &ast::Visibility, span: Span, ) -> Option { // Drop semicolon or it will be interpreted as comment. @@ -333,7 +335,7 @@ impl<'a> FmtVisitor<'a> { &context, indent, ident, - &FnSig::from_method_sig(sig, generics, DEFAULT_VISIBILITY), + &FnSig::from_method_sig(sig, generics, vis.clone()), span, FnBraceStyle::None, )?; diff --git a/src/visitor.rs b/src/visitor.rs index 079568630cf..9642c481641 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -569,6 +569,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { item.ident, &fn_signature, generics, + &item.vis, item.span, ); self.push_rewrite(item.span, rewrite); @@ -639,13 +640,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)), ast::AssocItemKind::Fn(ref fn_kind) => { let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind; + let vis = ast::Visibility { + kind: ast::VisibilityKind::Inherited, + span: DUMMY_SP, + tokens: None, + }; if let Some(ref body) = block { let inner_attrs = inner_attributes(&ti.attrs); - let vis = ast::Visibility { - kind: ast::VisibilityKind::Inherited, - span: DUMMY_SP, - tokens: None, - }; let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait); self.visit_fn( visit::FnKind::Fn(fn_ctxt, ti.ident, sig, &vis, Some(body)), @@ -658,7 +659,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } else { let indent = self.block_indent; let rewrite = - self.rewrite_required_fn(indent, ti.ident, sig, generics, ti.span); + self.rewrite_required_fn(indent, ti.ident, sig, generics, &vis, ti.span); self.push_rewrite(ti.span, rewrite); } } @@ -708,7 +709,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } else { let indent = self.block_indent; let rewrite = - self.rewrite_required_fn(indent, ii.ident, sig, generics, ii.span); + self.rewrite_required_fn(indent, ii.ident, sig, generics, &ii.vis, ii.span); self.push_rewrite(ii.span, rewrite); } } diff --git a/tests/target/fn-without-body.rs b/tests/target/fn-without-body.rs new file mode 100644 index 00000000000..e3fe5959d9a --- /dev/null +++ b/tests/target/fn-without-body.rs @@ -0,0 +1,9 @@ +// Tests fns without function body + +pub fn foo(a: AAA, b: BBB) -> RetType; + +pub(crate) fn foo(a: AAA, b: BBB) -> RetType; + +impl Foo { + pub fn foo(a: AAA); +}