Skip to content

Commit 8cc4ab6

Browse files
feat: support parameter attributes
1 parent dfe87fe commit 8cc4ab6

File tree

3 files changed

+156
-15
lines changed

3 files changed

+156
-15
lines changed

src/items.rs

+66-15
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,32 @@ 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+
.to_vec()
1911+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1912+
let span = if !self.attrs.is_empty() {
1913+
mk_sp(
1914+
self.attrs[self.attrs.len() - 1].span.hi(),
1915+
self.pat.span.lo(),
1916+
)
1917+
} else {
1918+
mk_sp(self.span.lo(), self.span.lo())
1919+
};
1920+
19081921
if let Some(ref explicit_self) = self.to_self() {
1909-
rewrite_explicit_self(context, explicit_self)
1922+
rewrite_explicit_self(context, explicit_self, &param_attrs_result, span, shape)
19101923
} else if is_named_arg(self) {
1911-
let mut result = self
1912-
.pat
1913-
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1924+
let mut result = combine_strs_with_missing_comments(
1925+
context,
1926+
&param_attrs_result,
1927+
&self
1928+
.pat
1929+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
1930+
span,
1931+
shape,
1932+
true,
1933+
)?;
19141934

19151935
if !is_empty_infer(&*self.ty, self.pat.span) {
19161936
let (before_comment, after_comment) =
@@ -1936,6 +1956,9 @@ impl Rewrite for ast::Param {
19361956
fn rewrite_explicit_self(
19371957
context: &RewriteContext<'_>,
19381958
explicit_self: &ast::ExplicitSelf,
1959+
param_attrs: &str,
1960+
span: Span,
1961+
shape: Shape,
19391962
) -> Option<String> {
19401963
match explicit_self.node {
19411964
ast::SelfKind::Region(lt, m) => {
@@ -1946,9 +1969,23 @@ fn rewrite_explicit_self(
19461969
context,
19471970
Shape::legacy(context.config.max_width(), Indent::empty()),
19481971
)?;
1949-
Some(format!("&{} {}self", lifetime_str, mut_str))
1972+
Some(combine_strs_with_missing_comments(
1973+
context,
1974+
&param_attrs,
1975+
&format!("&{} {}self", lifetime_str, mut_str),
1976+
span,
1977+
shape,
1978+
true,
1979+
)?)
19501980
}
1951-
None => Some(format!("&{}self", mut_str)),
1981+
None => Some(combine_strs_with_missing_comments(
1982+
context,
1983+
&param_attrs,
1984+
&format!("&{}self", mut_str),
1985+
span,
1986+
shape,
1987+
true,
1988+
)?),
19521989
}
19531990
}
19541991
ast::SelfKind::Explicit(ref ty, mutability) => {
@@ -1957,21 +1994,35 @@ fn rewrite_explicit_self(
19571994
Shape::legacy(context.config.max_width(), Indent::empty()),
19581995
)?;
19591996

1960-
Some(format!(
1961-
"{}self: {}",
1962-
format_mutability(mutability),
1963-
type_str
1964-
))
1997+
Some(combine_strs_with_missing_comments(
1998+
context,
1999+
&param_attrs,
2000+
&format!("{}self: {}", format_mutability(mutability), type_str),
2001+
span,
2002+
shape,
2003+
true,
2004+
)?)
19652005
}
1966-
ast::SelfKind::Value(mutability) => Some(format!("{}self", format_mutability(mutability))),
2006+
ast::SelfKind::Value(mutability) => Some(combine_strs_with_missing_comments(
2007+
context,
2008+
&param_attrs,
2009+
&format!("{}self", format_mutability(mutability)),
2010+
span,
2011+
shape,
2012+
true,
2013+
)?),
19672014
}
19682015
}
19692016

19702017
pub(crate) fn span_lo_for_arg(arg: &ast::Param) -> BytePos {
1971-
if is_named_arg(arg) {
1972-
arg.pat.span.lo()
2018+
if arg.attrs.is_empty() {
2019+
if is_named_arg(arg) {
2020+
arg.pat.span.lo()
2021+
} else {
2022+
arg.ty.span.lo()
2023+
}
19732024
} else {
1974-
arg.ty.span.lo()
2025+
arg.attrs[0].span.lo()
19752026
}
19762027
}
19772028

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)