Skip to content

Commit fcbf4ac

Browse files
authored
Rollup merge of #131546 - surechen:fix_129833, r=jieyouxu
Make unused_parens's suggestion considering expr's attributes. For the expr with attributes, like `let _ = (#[inline] || println!("Hello!"));`, the suggestion's span should contains the attributes, or the suggestion will remove them. fixes #129833
2 parents 9e72070 + 1e8d6d1 commit fcbf4ac

4 files changed

+70
-1
lines changed

compiler/rustc_lint/src/unused.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,15 @@ trait UnusedDelimLint {
804804
.find_ancestor_inside(value.span)
805805
.map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))),
806806
ast::ExprKind::Paren(ref expr) => {
807-
expr.span.find_ancestor_inside(value.span).map(|expr_span| {
807+
// For the expr with attributes, like `let _ = (#[inline] || println!("Hello!"));`,
808+
// the span should contains the attributes, or the suggestion will remove them.
809+
let expr_span_with_attrs =
810+
if let Some(attr_lo) = expr.attrs.iter().map(|attr| attr.span.lo()).min() {
811+
expr.span.with_lo(attr_lo)
812+
} else {
813+
expr.span
814+
};
815+
expr_span_with_attrs.find_ancestor_inside(value.span).map(|expr_span| {
808816
(value.span.with_hi(expr_span.lo()), value.span.with_lo(expr_span.hi()))
809817
})
810818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ run-rustfix
2+
// Check the `unused_parens` suggestion for paren_expr with attributes.
3+
// The suggestion should retain attributes in the front.
4+
5+
#![feature(stmt_expr_attributes)]
6+
#![deny(unused_parens)]
7+
8+
pub fn foo() -> impl Fn() {
9+
let _ = #[inline] #[allow(dead_code)] || println!("Hello!"); //~ERROR unnecessary parentheses
10+
#[inline] #[allow(dead_code)] || println!("Hello!") //~ERROR unnecessary parentheses
11+
}
12+
13+
fn main() {
14+
let _ = foo();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ run-rustfix
2+
// Check the `unused_parens` suggestion for paren_expr with attributes.
3+
// The suggestion should retain attributes in the front.
4+
5+
#![feature(stmt_expr_attributes)]
6+
#![deny(unused_parens)]
7+
8+
pub fn foo() -> impl Fn() {
9+
let _ = (#[inline] #[allow(dead_code)] || println!("Hello!")); //~ERROR unnecessary parentheses
10+
(#[inline] #[allow(dead_code)] || println!("Hello!")) //~ERROR unnecessary parentheses
11+
}
12+
13+
fn main() {
14+
let _ = foo();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: unnecessary parentheses around assigned value
2+
--> $DIR/unused-parens-for-stmt-expr-attributes-issue-129833.rs:9:13
3+
|
4+
LL | let _ = (#[inline] #[allow(dead_code)] || println!("Hello!"));
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-parens-for-stmt-expr-attributes-issue-129833.rs:6:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - let _ = (#[inline] #[allow(dead_code)] || println!("Hello!"));
15+
LL + let _ = #[inline] #[allow(dead_code)] || println!("Hello!");
16+
|
17+
18+
error: unnecessary parentheses around block return value
19+
--> $DIR/unused-parens-for-stmt-expr-attributes-issue-129833.rs:10:5
20+
|
21+
LL | (#[inline] #[allow(dead_code)] || println!("Hello!"))
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - (#[inline] #[allow(dead_code)] || println!("Hello!"))
27+
LL + #[inline] #[allow(dead_code)] || println!("Hello!")
28+
|
29+
30+
error: aborting due to 2 previous errors
31+

0 commit comments

Comments
 (0)