Skip to content

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

Closed
@zachs18

Description

@zachs18

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-stmt_expr_attributes`#![feature(stmt_expr_attributes)]`L-unused_parensLint: unused_parensT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions