summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorTrevor Gross <t.gross35@gmail.com>2024-10-11 23:57:46 -0400
committerGitHub <noreply@github.com>2024-10-11 23:57:46 -0400
commitfcbf4ac6f9071baa2a48799d697cbf74f81f30e1 (patch)
tree91c5555fc6077be0b7eca1ab2fa87ec8f85ea6db /compiler
parent9e72070f77b0d5a71dca91f359b599bab150e3e5 (diff)
parent1e8d6d1ec3cad6bc7d88c0a8a993f69835e8422f (diff)
downloadrust-fcbf4ac6f9071baa2a48799d697cbf74f81f30e1.tar.gz
rust-fcbf4ac6f9071baa2a48799d697cbf74f81f30e1.zip
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
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_lint/src/unused.rs10
1 files changed, 9 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index 12d5b5cf979..1a007250961 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -804,7 +804,15 @@ trait UnusedDelimLint {
                 .find_ancestor_inside(value.span)
                 .map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))),
             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()))
                 })
             }