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
This commit is contained in:
surechen 2024-10-11 20:27:46 +08:00
parent 1b5aa96d60
commit 1e8d6d1ec3
4 changed files with 70 additions and 1 deletions

View File

@ -804,7 +804,15 @@ fn visit_expr(&mut self, expr: &'ast ast::Expr) -> ControlFlow<()> {
.find_ancestor_inside(value.span) .find_ancestor_inside(value.span)
.map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))), .map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))),
ast::ExprKind::Paren(ref expr) => { ast::ExprKind::Paren(ref expr) => {
expr.span.find_ancestor_inside(value.span).map(|expr_span| { // For the expr with attributes, like `let _ = (#[inline] || println!("Hello!"));`,
// the span should contains the attributes, or the suggestion will remove them.
let expr_span_with_attrs =
if let Some(attr_lo) = expr.attrs.iter().map(|attr| attr.span.lo()).min() {
expr.span.with_lo(attr_lo)
} else {
expr.span
};
expr_span_with_attrs.find_ancestor_inside(value.span).map(|expr_span| {
(value.span.with_hi(expr_span.lo()), value.span.with_lo(expr_span.hi())) (value.span.with_hi(expr_span.lo()), value.span.with_lo(expr_span.hi()))
}) })
} }

View File

@ -0,0 +1,15 @@
//@ run-rustfix
// Check the `unused_parens` suggestion for paren_expr with attributes.
// The suggestion should retain attributes in the front.
#![feature(stmt_expr_attributes)]
#![deny(unused_parens)]
pub fn foo() -> impl Fn() {
let _ = #[inline] #[allow(dead_code)] || println!("Hello!"); //~ERROR unnecessary parentheses
#[inline] #[allow(dead_code)] || println!("Hello!") //~ERROR unnecessary parentheses
}
fn main() {
let _ = foo();
}

View File

@ -0,0 +1,15 @@
//@ run-rustfix
// Check the `unused_parens` suggestion for paren_expr with attributes.
// The suggestion should retain attributes in the front.
#![feature(stmt_expr_attributes)]
#![deny(unused_parens)]
pub fn foo() -> impl Fn() {
let _ = (#[inline] #[allow(dead_code)] || println!("Hello!")); //~ERROR unnecessary parentheses
(#[inline] #[allow(dead_code)] || println!("Hello!")) //~ERROR unnecessary parentheses
}
fn main() {
let _ = foo();
}

View File

@ -0,0 +1,31 @@
error: unnecessary parentheses around assigned value
--> $DIR/unused-parens-for-stmt-expr-attributes-issue-129833.rs:9:13
|
LL | let _ = (#[inline] #[allow(dead_code)] || println!("Hello!"));
| ^ ^
|
note: the lint level is defined here
--> $DIR/unused-parens-for-stmt-expr-attributes-issue-129833.rs:6:9
|
LL | #![deny(unused_parens)]
| ^^^^^^^^^^^^^
help: remove these parentheses
|
LL - let _ = (#[inline] #[allow(dead_code)] || println!("Hello!"));
LL + let _ = #[inline] #[allow(dead_code)] || println!("Hello!");
|
error: unnecessary parentheses around block return value
--> $DIR/unused-parens-for-stmt-expr-attributes-issue-129833.rs:10:5
|
LL | (#[inline] #[allow(dead_code)] || println!("Hello!"))
| ^ ^
|
help: remove these parentheses
|
LL - (#[inline] #[allow(dead_code)] || println!("Hello!"))
LL + #[inline] #[allow(dead_code)] || println!("Hello!")
|
error: aborting due to 2 previous errors