Skip to content

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

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
OliveIsAWord opened this issue Jun 5, 2022 · 5 comments
Closed

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

OliveIsAWord opened this issue Jun 5, 2022 · 5 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@OliveIsAWord
Copy link

OliveIsAWord commented Jun 5, 2022

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.

@OliveIsAWord OliveIsAWord added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 5, 2022
@est31
Copy link
Member

est31 commented Jun 5, 2022

For #97266 I've recently become very familiar with the code that determines lint levels. @rustbot claim

@ehuss
Copy link
Contributor

ehuss commented Jun 5, 2022

I believe the arithmetic_overflow example is because attributes bind tighter than any other operator, so the attribute is part of the expression 200, not 200 + 200. You have to place the addition expression in parenthesis for it to work. See https://rust-lang.github.io/rfcs/0016-more-attributes.html for more.

@est31
Copy link
Member

est31 commented Jun 5, 2022

Oh yeah good point

@RodBurman
Copy link

RodBurman commented Mar 6, 2025

Should the issue be closed then?

@ehuss
Copy link
Contributor

ehuss commented Mar 6, 2025

Yea, I'm going to close in favor of #127436 where this very issue is being decided. I think people understand the ambiguity can be confusing, and will likely not be stabilized as-is without some mitigations.

@ehuss ehuss closed this as completed Mar 6, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants