Skip to content

Commit 269a7c6

Browse files
feat: support parameter attributes
1 parent dfe87fe commit 269a7c6

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

src/items.rs

+31-11
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,22 @@ fn get_missing_arg_comments(
19051905

19061906
impl Rewrite for ast::Param {
19071907
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1908+
let mut param_result = self
1909+
.attrs
1910+
.to_vec()
1911+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1912+
if !self.attrs.is_empty() {
1913+
param_result = format!("{} ", param_result);
1914+
}
19081915
if let Some(ref explicit_self) = self.to_self() {
1909-
rewrite_explicit_self(context, explicit_self)
1916+
rewrite_explicit_self(context, explicit_self, &param_result)
19101917
} else if is_named_arg(self) {
1911-
let mut result = self
1912-
.pat
1913-
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1918+
let mut result = format!(
1919+
"{}{}",
1920+
param_result,
1921+
self.pat
1922+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
1923+
);
19141924

19151925
if !is_empty_infer(&*self.ty, self.pat.span) {
19161926
let (before_comment, after_comment) =
@@ -1936,6 +1946,7 @@ impl Rewrite for ast::Param {
19361946
fn rewrite_explicit_self(
19371947
context: &RewriteContext<'_>,
19381948
explicit_self: &ast::ExplicitSelf,
1949+
param_attrs: &str,
19391950
) -> Option<String> {
19401951
match explicit_self.node {
19411952
ast::SelfKind::Region(lt, m) => {
@@ -1946,9 +1957,9 @@ fn rewrite_explicit_self(
19461957
context,
19471958
Shape::legacy(context.config.max_width(), Indent::empty()),
19481959
)?;
1949-
Some(format!("&{} {}self", lifetime_str, mut_str))
1960+
Some(format!("{}&{} {}self", param_attrs, lifetime_str, mut_str))
19501961
}
1951-
None => Some(format!("&{}self", mut_str)),
1962+
None => Some(format!("{}&{}self", param_attrs, mut_str)),
19521963
}
19531964
}
19541965
ast::SelfKind::Explicit(ref ty, mutability) => {
@@ -1958,20 +1969,29 @@ fn rewrite_explicit_self(
19581969
)?;
19591970

19601971
Some(format!(
1961-
"{}self: {}",
1972+
"{}{}self: {}",
1973+
param_attrs,
19621974
format_mutability(mutability),
19631975
type_str
19641976
))
19651977
}
1966-
ast::SelfKind::Value(mutability) => Some(format!("{}self", format_mutability(mutability))),
1978+
ast::SelfKind::Value(mutability) => Some(format!(
1979+
"{}{}self",
1980+
param_attrs,
1981+
format_mutability(mutability)
1982+
)),
19671983
}
19681984
}
19691985

19701986
pub(crate) fn span_lo_for_arg(arg: &ast::Param) -> BytePos {
1971-
if is_named_arg(arg) {
1972-
arg.pat.span.lo()
1987+
if arg.attrs.is_empty() {
1988+
if is_named_arg(arg) {
1989+
arg.pat.span.lo()
1990+
} else {
1991+
arg.ty.span.lo()
1992+
}
19731993
} else {
1974-
arg.ty.span.lo()
1994+
arg.attrs[0].span.lo()
19751995
}
19761996
}
19771997

tests/source/fn-param-attributes.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// https://github.com/rust-lang/rustfmt/issues/3623
2+
3+
fn foo(#[cfg(something)] x: i32, y: i32) -> i32 {
4+
x + y
5+
}
6+
7+
fn foo_b(#[cfg(something)]x: i32, y: i32) -> i32 {
8+
x + y
9+
}
10+
11+
fn add(#[cfg(something)]#[deny(C)] x: i32, y: i32) -> i32 {
12+
x + y
13+
}
14+
15+
struct NamedSelfRefStruct {}
16+
impl NamedSelfRefStruct {
17+
fn foo(
18+
#[cfg(something)] self: &Self,
19+
) {}
20+
}
21+
22+
struct MutStruct {}
23+
impl MutStruct {
24+
fn foo(
25+
#[cfg(foo)]&mut self,#[deny(C)] b: i32,
26+
) {}
27+
}
28+
29+
fn main() {
30+
let c = |
31+
#[allow(C)]a: u32,
32+
#[cfg(something)] b: i32,
33+
#[cfg_attr(something, cfg(nothing))]#[deny(C)] c: i32,
34+
| {};
35+
let _ = c(1, 2);
36+
}
37+
38+
pub fn bar(
39+
/// bar
40+
#[test] a: u32,
41+
/// Bar
42+
#[must_use]
43+
/// Baz
44+
#[no_mangle] b: i32,
45+
) {}

tests/target/fn-param-attributes.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// https://github.com/rust-lang/rustfmt/issues/3623
2+
3+
fn foo(#[cfg(something)] x: i32, y: i32) -> i32 {
4+
x + y
5+
}
6+
7+
fn foo_b(#[cfg(something)] x: i32, y: i32) -> i32 {
8+
x + y
9+
}
10+
11+
fn add(
12+
#[cfg(something)]
13+
#[deny(C)] x: i32,
14+
y: i32,
15+
) -> i32 {
16+
x + y
17+
}
18+
19+
struct NamedSelfRefStruct {}
20+
impl NamedSelfRefStruct {
21+
fn foo(#[cfg(something)] self: &Self) {}
22+
}
23+
24+
struct MutStruct {}
25+
impl MutStruct {
26+
fn foo(#[cfg(foo)] &mut self, #[deny(C)] b: i32) {}
27+
}
28+
29+
fn main() {
30+
let c = |#[allow(C)] a: u32,
31+
#[cfg(something)] b: i32,
32+
#[cfg_attr(something, cfg(nothing))]
33+
#[deny(C)] c: i32| {};
34+
let _ = c(1, 2);
35+
}
36+
37+
pub fn bar(
38+
/// bar
39+
#[test] a: u32,
40+
/// Bar
41+
#[must_use]
42+
/// Baz
43+
#[no_mangle] b: i32,
44+
) {
45+
}

0 commit comments

Comments
 (0)