Skip to content

Preserve visibility of fn items without body #4858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ impl<'a> FmtVisitor<'a> {
ident: Ident,
sig: &ast::FnSig,
generics: &ast::Generics,
vis: &ast::Visibility,
span: Span,
) -> Option<String> {
// Drop semicolon or it will be interpreted as comment.
Expand All @@ -333,7 +334,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,
)?;
Expand Down
15 changes: 8 additions & 7 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)),
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/target/fn-without-body.rs
Original file line number Diff line number Diff line change
@@ -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);
}