diff --git a/src/items.rs b/src/items.rs index 613f9b5b729..83ecfd586fc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1905,12 +1905,32 @@ fn get_missing_arg_comments( impl Rewrite for ast::Param { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + let param_attrs_result = self + .attrs + .to_vec() + .rewrite(context, Shape::legacy(shape.width, shape.indent))?; + let span = if !self.attrs.is_empty() { + mk_sp( + self.attrs[self.attrs.len() - 1].span.hi(), + self.pat.span.lo(), + ) + } else { + mk_sp(self.span.lo(), self.span.lo()) + }; + if let Some(ref explicit_self) = self.to_self() { - rewrite_explicit_self(context, explicit_self) + rewrite_explicit_self(context, explicit_self, ¶m_attrs_result, span, shape) } else if is_named_arg(self) { - let mut result = self - .pat - .rewrite(context, Shape::legacy(shape.width, shape.indent))?; + let mut result = combine_strs_with_missing_comments( + context, + ¶m_attrs_result, + &self + .pat + .rewrite(context, Shape::legacy(shape.width, shape.indent))?, + span, + shape, + true, + )?; if !is_empty_infer(&*self.ty, self.pat.span) { let (before_comment, after_comment) = @@ -1936,6 +1956,9 @@ impl Rewrite for ast::Param { fn rewrite_explicit_self( context: &RewriteContext<'_>, explicit_self: &ast::ExplicitSelf, + param_attrs: &str, + span: Span, + shape: Shape, ) -> Option { match explicit_self.node { ast::SelfKind::Region(lt, m) => { @@ -1946,9 +1969,23 @@ fn rewrite_explicit_self( context, Shape::legacy(context.config.max_width(), Indent::empty()), )?; - Some(format!("&{} {}self", lifetime_str, mut_str)) + Some(combine_strs_with_missing_comments( + context, + ¶m_attrs, + &format!("&{} {}self", lifetime_str, mut_str), + span, + shape, + true, + )?) } - None => Some(format!("&{}self", mut_str)), + None => Some(combine_strs_with_missing_comments( + context, + ¶m_attrs, + &format!("&{}self", mut_str), + span, + shape, + true, + )?), } } ast::SelfKind::Explicit(ref ty, mutability) => { @@ -1957,21 +1994,35 @@ fn rewrite_explicit_self( Shape::legacy(context.config.max_width(), Indent::empty()), )?; - Some(format!( - "{}self: {}", - format_mutability(mutability), - type_str - )) + Some(combine_strs_with_missing_comments( + context, + ¶m_attrs, + &format!("{}self: {}", format_mutability(mutability), type_str), + span, + shape, + true, + )?) } - ast::SelfKind::Value(mutability) => Some(format!("{}self", format_mutability(mutability))), + ast::SelfKind::Value(mutability) => Some(combine_strs_with_missing_comments( + context, + ¶m_attrs, + &format!("{}self", format_mutability(mutability)), + span, + shape, + true, + )?), } } pub(crate) fn span_lo_for_arg(arg: &ast::Param) -> BytePos { - if is_named_arg(arg) { - arg.pat.span.lo() + if arg.attrs.is_empty() { + if is_named_arg(arg) { + arg.pat.span.lo() + } else { + arg.ty.span.lo() + } } else { - arg.ty.span.lo() + arg.attrs[0].span.lo() } } diff --git a/tests/source/fn-param-attributes.rs b/tests/source/fn-param-attributes.rs new file mode 100644 index 00000000000..c4d7f9c1b49 --- /dev/null +++ b/tests/source/fn-param-attributes.rs @@ -0,0 +1,45 @@ +// https://github.com/rust-lang/rustfmt/issues/3623 + +fn foo(#[cfg(something)] x: i32, y: i32) -> i32 { + x + y +} + +fn foo_b(#[cfg(something)]x: i32, y: i32) -> i32 { + x + y +} + +fn add(#[cfg(something)]#[deny(C)] x: i32, y: i32) -> i32 { + x + y +} + +struct NamedSelfRefStruct {} +impl NamedSelfRefStruct { + fn foo( +#[cfg(something)] self: &Self, + ) {} +} + +struct MutStruct {} +impl MutStruct { + fn foo( + #[cfg(foo)]&mut self,#[deny(C)] b: i32, + ) {} +} + +fn main() { + let c = | + #[allow(C)]a: u32, + #[cfg(something)] b: i32, + #[cfg_attr(something, cfg(nothing))]#[deny(C)] c: i32, + | {}; + let _ = c(1, 2); +} + +pub fn bar( + /// bar +#[test] a: u32, + /// Bar + #[must_use] +/// Baz + #[no_mangle] b: i32, +) {} diff --git a/tests/target/fn-param-attributes.rs b/tests/target/fn-param-attributes.rs new file mode 100644 index 00000000000..b7bef23b9a7 --- /dev/null +++ b/tests/target/fn-param-attributes.rs @@ -0,0 +1,45 @@ +// https://github.com/rust-lang/rustfmt/issues/3623 + +fn foo(#[cfg(something)] x: i32, y: i32) -> i32 { + x + y +} + +fn foo_b(#[cfg(something)] x: i32, y: i32) -> i32 { + x + y +} + +fn add( + #[cfg(something)] + #[deny(C)] x: i32, + y: i32, +) -> i32 { + x + y +} + +struct NamedSelfRefStruct {} +impl NamedSelfRefStruct { + fn foo(#[cfg(something)] self: &Self) {} +} + +struct MutStruct {} +impl MutStruct { + fn foo(#[cfg(foo)] &mut self, #[deny(C)] b: i32) {} +} + +fn main() { + let c = |#[allow(C)] a: u32, + #[cfg(something)] b: i32, + #[cfg_attr(something, cfg(nothing))] + #[deny(C)] c: i32| {}; + let _ = c(1, 2); +} + +pub fn bar( + /// bar + #[test] a: u32, + /// Bar + #[must_use] + /// Baz + #[no_mangle] b: i32, +) { +}