Skip to content

#[allow(...)] doesn't work on expressions #97748

Closed as duplicate of#127436
Closed as duplicate of#127436
@OliveIsAWord

Description

@OliveIsAWord

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=01f0dcf3b636948e5c1cf8baeeedb35c

#![feature(stmt_expr_attributes)]

// Compiles successfully
#[allow(arithmetic_overflow)]
pub fn attr_on_func() -> u8 {
    200 + 200
}

// Yields compiler error
pub fn attr_on_expr() -> u8 {
    #[allow(arithmetic_overflow)]
    200 + 200
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: this arithmetic operation will overflow
  --> src/lib.rs:11:5
   |
11 | /     #[allow(arithmetic_overflow)]
12 | |     200 + 200
   | |_____________^ attempt to compute `200_u8 + 200_u8`, which would overflow
   |
   = note: `#[deny(arithmetic_overflow)]` on by default

error: could not compile `playground` due to previous error

The same thing occurs for clippy lints:

// Lint correctly silenced
#[allow(clippy::unused_unit)]
pub fn clippy_attr_on_func() {
    ()
}

// Lint erroneously not silenced
pub fn clippy_attr_on_expr() {
    #[allow(clippy::unused_unit)]
    ()
}
warning: unneeded unit expression
  --> src\lib.rs:10:5
   |
10 |     ()
   |     ^^ help: remove the final `()`
   |
   = note: `#[warn(clippy::unused_unit)]` on by default

To reiterate: the issue is that attributes for silencing warnings are ignored when placed on expressions. I realize now the first example might be an issue with higher precedence, but the second example with clippy is obviously faulty.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions