Skip to content

unused_parens lint suggestion also removes attributes on inner expression with feature(stmt_expr_attributes). #129833

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
zachs18 opened this issue Aug 31, 2024 · 2 comments · Fixed by #131546
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` L-unused_parens Lint: unused_parens requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zachs18
Copy link
Contributor

zachs18 commented Aug 31, 2024

I tried this code:

#![feature(stmt_expr_attributes)]
#![allow(unused_must_use)]

pub fn foo() -> impl Fn() {
    let _ = (#[inline] || println!("Hello!"));
    (#[inline] || println!("Hello!"));
    (#[inline] || println!("Hello!"))
}

I expected to see this happen: The unused_parens warning fires on all three lines of foo, and the suggestions should only suggest removing the parentheses, not the attribute also.

Instead, this happened: The unused_parens fires on the first and last lines of foo, and the suggestion also removes the #[inline].
No unused_parens warning fires on the second line (#[inline] || println!("Hello!")); (but this is probably a separate issue, unrelated to attributes; it happens even when the attribute is removed; maybe unused_must_use overrides it or something).

warning: unnecessary parentheses around assigned value
 --> a.rs:5:13
  |
5 |     let _ = (#[inline] || println!("Hello!"));
  |             ^^^^^^^^^^^                     ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
5 -     let _ = (#[inline] || println!("Hello!"));
5 +     let _ = || println!("Hello!");
  |

warning: unnecessary parentheses around block return value
 --> a.rs:7:5
  |
7 |     (#[inline] || println!("Hello!"))
  |     ^^^^^^^^^^^                     ^
  |
help: remove these parentheses
  |
7 -     (#[inline] || println!("Hello!"))
7 +     || println!("Hello!")
  |

warning: 2 warnings emitted

Meta

rustc --version --verbose:

rustc 1.82.0-nightly (0d634185d 2024-08-29)
binary: rustc
commit-hash: 0d634185dfddefe09047881175f35c65d68dcff1
commit-date: 2024-08-29
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

Requires use of #![feature(stmt_expr_attributes)], otherwise the #[inline] is not legal in that position (though note that the unused_parens warning and suggestion is the same on stable, there's just an error[E0658]: attributes on expressions are experimental first).

The issue is not specific to closures and #[inline], but that was the simplest example of a reasonable attribute-on-expression I thought of.

@rustbot label +A-diagnostics +requires-nightly

@zachs18 zachs18 added the C-bug Category: This is a bug. label Aug 31, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-diagnostics Area: Messages for errors, warnings, and lints requires-nightly This issue requires a nightly compiler in some way. labels Aug 31, 2024
@zachs18

This comment was marked as resolved.

@jieyouxu jieyouxu added F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` L-unused_parens Lint: unused_parens A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Aug 31, 2024
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 31, 2024
@surechen
Copy link
Contributor

@rustbot claim

surechen added a commit to surechen/rust that referenced this issue Oct 11, 2024
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 rust-lang#129833
tgross35 added a commit to tgross35/rust that referenced this issue Oct 12, 2024
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 rust-lang#129833
@bors bors closed this as completed in 1e8d6d1 Oct 12, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 12, 2024
Rollup merge of rust-lang#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 rust-lang#129833
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` L-unused_parens Lint: unused_parens requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants