Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

#[expect(...)] attribute not working for clippy::assertion_on_constant #13729

Open
ethanmsl opened this issue Nov 25, 2024 · 1 comment
Open
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@ethanmsl
Copy link

ethanmsl commented Nov 25, 2024

Summary

#[expect(clippy::assertions_on_constants)]

Does not work to prevent clippy warning for same.
(Not sure if clippy warnings use compiled code or just raw code; if latter perhaps ordering of run is relevant - with expect notation being applied to code that's been optimized away?)

e.g.

#[cfg(test)]
mod tests {
        #[test]
        fn test_truth() {
                #[expect(clippy::assertions_on_constants)]
                assert!(true);
        }
}

Reproducer

I tried this code:

fn main() {
        // unused attribute `expect`
        // `#[warn(unused_attributes)]` on by default
        #[expect(clippy::assertions_on_constants)]
        assert!(true);
        // `assert!(true)` will be optimized out by the compiler
        // remove it
        // for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
        // `#[warn(clippy::assertions_on_constants)]` on by default
}

I expected to see this happen:
the expect annotation of clippy warning prevent the clippy warning

Instead, this happened:
clippy warning remained and a new clippy warning mentioning that the expect had no effect

Version

rustc 1.85.0-nightly (28fc2ba71 2024-11-24)
binary: rustc
commit-hash: 28fc2ba7142654fa6e654926f96ff913027b200e
commit-date: 2024-11-24
host: aarch64-apple-darwin
release: 1.85.0-nightly
LLVM version: 19.1.4

Note, if relevant:
cargo.toml also contains:

cargo-features = ["edition2024"]
#...
[package]
#...
edition = "2024"

Additional Labels

No response

@ethanmsl ethanmsl added the C-bug Category: Clippy is not doing the correct thing label Nov 25, 2024
@ethanmsl
Copy link
Author

ethanmsl commented Nov 25, 2024

On further investigation this appears to be related to the currently open issue: rust-clippy/issues/10355

Looking at the clippy warning in terminal gave additional context:

note: the built-in attribute `expect` will be ignored, since it's applied to the macro invocation `assert`
 --> src/bin/min_example.rs:5:9
  |
5 |         assert!(true);
  |         ^^^^^^
  = note: `#[warn(unused_attributes)]` on by default

The underlying issue then appears to simple be that attributes are not applied to macros.
I'll leave this here for someone else to close as redundant with the above issue (10355), just in case.


A workaround is simple to apply the #[expect(...)] to brackets {...} around the macro:

        #[expect(clippy::assertions_on_constants)]
        {
                assert!(true);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

1 participant