Skip to content

Commit 846ac43

Browse files
feat: support parameter attributes
1 parent dfe87fe commit 846ac43

File tree

3 files changed

+195
-15
lines changed

3 files changed

+195
-15
lines changed

src/items.rs

+74-15
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,39 @@ fn get_missing_arg_comments(
19051905

19061906
impl Rewrite for ast::Param {
19071907
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1908+
let param_attrs_result = self
1909+
.attrs
1910+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1911+
let (span, has_multiple_attr_lines) = if !self.attrs.is_empty() {
1912+
let num_attrs = self.attrs.len();
1913+
(
1914+
mk_sp(self.attrs[num_attrs - 1].span.hi(), self.pat.span.lo()),
1915+
param_attrs_result.matches("\n").count() > 0,
1916+
)
1917+
} else {
1918+
(mk_sp(self.span.lo(), self.span.lo()), false)
1919+
};
1920+
19081921
if let Some(ref explicit_self) = self.to_self() {
1909-
rewrite_explicit_self(context, explicit_self)
1922+
rewrite_explicit_self(
1923+
context,
1924+
explicit_self,
1925+
&param_attrs_result,
1926+
span,
1927+
shape,
1928+
has_multiple_attr_lines,
1929+
)
19101930
} else if is_named_arg(self) {
1911-
let mut result = self
1912-
.pat
1913-
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1931+
let mut result = combine_strs_with_missing_comments(
1932+
context,
1933+
&param_attrs_result,
1934+
&self
1935+
.pat
1936+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
1937+
span,
1938+
shape,
1939+
!has_multiple_attr_lines,
1940+
)?;
19141941

19151942
if !is_empty_infer(&*self.ty, self.pat.span) {
19161943
let (before_comment, after_comment) =
@@ -1936,6 +1963,10 @@ impl Rewrite for ast::Param {
19361963
fn rewrite_explicit_self(
19371964
context: &RewriteContext<'_>,
19381965
explicit_self: &ast::ExplicitSelf,
1966+
param_attrs: &str,
1967+
span: Span,
1968+
shape: Shape,
1969+
has_multiple_attr_lines: bool,
19391970
) -> Option<String> {
19401971
match explicit_self.node {
19411972
ast::SelfKind::Region(lt, m) => {
@@ -1946,9 +1977,23 @@ fn rewrite_explicit_self(
19461977
context,
19471978
Shape::legacy(context.config.max_width(), Indent::empty()),
19481979
)?;
1949-
Some(format!("&{} {}self", lifetime_str, mut_str))
1980+
Some(combine_strs_with_missing_comments(
1981+
context,
1982+
&param_attrs,
1983+
&format!("&{} {}self", lifetime_str, mut_str),
1984+
span,
1985+
shape,
1986+
!has_multiple_attr_lines,
1987+
)?)
19501988
}
1951-
None => Some(format!("&{}self", mut_str)),
1989+
None => Some(combine_strs_with_missing_comments(
1990+
context,
1991+
&param_attrs,
1992+
&format!("&{}self", mut_str),
1993+
span,
1994+
shape,
1995+
!has_multiple_attr_lines,
1996+
)?),
19521997
}
19531998
}
19541999
ast::SelfKind::Explicit(ref ty, mutability) => {
@@ -1957,21 +2002,35 @@ fn rewrite_explicit_self(
19572002
Shape::legacy(context.config.max_width(), Indent::empty()),
19582003
)?;
19592004

1960-
Some(format!(
1961-
"{}self: {}",
1962-
format_mutability(mutability),
1963-
type_str
1964-
))
2005+
Some(combine_strs_with_missing_comments(
2006+
context,
2007+
&param_attrs,
2008+
&format!("{}self: {}", format_mutability(mutability), type_str),
2009+
span,
2010+
shape,
2011+
!has_multiple_attr_lines,
2012+
)?)
19652013
}
1966-
ast::SelfKind::Value(mutability) => Some(format!("{}self", format_mutability(mutability))),
2014+
ast::SelfKind::Value(mutability) => Some(combine_strs_with_missing_comments(
2015+
context,
2016+
&param_attrs,
2017+
&format!("{}self", format_mutability(mutability)),
2018+
span,
2019+
shape,
2020+
!has_multiple_attr_lines,
2021+
)?),
19672022
}
19682023
}
19692024

19702025
pub(crate) fn span_lo_for_arg(arg: &ast::Param) -> BytePos {
1971-
if is_named_arg(arg) {
1972-
arg.pat.span.lo()
2026+
if arg.attrs.is_empty() {
2027+
if is_named_arg(arg) {
2028+
arg.pat.span.lo()
2029+
} else {
2030+
arg.ty.span.lo()
2031+
}
19732032
} else {
1974-
arg.ty.span.lo()
2033+
arg.attrs[0].span.lo()
19752034
}
19762035
}
19772036

tests/source/fn-param-attributes.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
) {}
46+
47+
48+
fn abc(
49+
#[foo]
50+
#[bar] param: u32,
51+
) {
52+
// ...
53+
}
54+
55+
fn really_really_really_loooooooooooooooooooong(#[cfg(some_even_longer_config_feature_that_keeps_going_and_going_and_going_forever_and_ever_and_ever_on_and_on)] b: i32) {
56+
// ...
57+
}

tests/target/fn-param-attributes.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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)]
14+
x: i32,
15+
y: i32,
16+
) -> i32 {
17+
x + y
18+
}
19+
20+
struct NamedSelfRefStruct {}
21+
impl NamedSelfRefStruct {
22+
fn foo(#[cfg(something)] self: &Self) {}
23+
}
24+
25+
struct MutStruct {}
26+
impl MutStruct {
27+
fn foo(#[cfg(foo)] &mut self, #[deny(C)] b: i32) {}
28+
}
29+
30+
fn main() {
31+
let c = |#[allow(C)] a: u32,
32+
#[cfg(something)] b: i32,
33+
#[cfg_attr(something, cfg(nothing))]
34+
#[deny(C)]
35+
c: i32| {};
36+
let _ = c(1, 2);
37+
}
38+
39+
pub fn bar(
40+
/// bar
41+
#[test]
42+
a: u32,
43+
/// Bar
44+
#[must_use]
45+
/// Baz
46+
#[no_mangle]
47+
b: i32,
48+
) {
49+
}
50+
51+
fn abc(
52+
#[foo]
53+
#[bar]
54+
param: u32,
55+
) {
56+
// ...
57+
}
58+
59+
fn really_really_really_loooooooooooooooooooong(
60+
#[cfg(some_even_longer_config_feature_that_keeps_going_and_going_and_going_forever_and_ever_and_ever_on_and_on)]
61+
b: i32,
62+
) {
63+
// ...
64+
}

0 commit comments

Comments
 (0)