rust-lang / rust-lang/rust-clippy

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

Open
#13,729 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided main reproducer with Clippy and compare the assertions_on_constants warning with the unused expect warning. Trace the lint's handling of the #[expect(...)] attribute and make the annotation suppress the intended warning without producing an unused-attribute warning; verify the behavior with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.