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

assertions_on_constants should not trigger inside const blocks #12847

Closed
TimNN opened this issue May 25, 2024 · 2 comments · Fixed by #12840
Closed

assertions_on_constants should not trigger inside const blocks #12847

TimNN opened this issue May 25, 2024 · 2 comments · Fixed by #12840
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@TimNN
Copy link
Contributor

TimNN commented May 25, 2024

Summary

As of #11966, assertions_on_constants does not trigger for const _: () = assert!(expr).

In recent Rust versions, this can be more nicely written as const { assert!(expr) }.

IMO assertions in const blocks should be exempted from assertions_on_constants, because generally their entire purpose is to be evaluated at compile time.

Lint Name

assertions_on_constants

Reproducer

I tried this code:

pub fn foo() {
  const { assert!(c_int::BITS <= usize::BITS) }
}

I saw this happen:

warning: `assert!(true)` will be optimized out by the compiler
   |
39 |             const { assert!(c_int::BITS <= usize::BITS) }
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: remove it
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
   = note: `#[warn(clippy::assertions_on_constants)]` on by default

I expected to see this happen:

No warnings.

Version

rustc 1.80.0-nightly (36153f1a4 2024-05-24)
binary: rustc
commit-hash: 36153f1a4e3162f0a143c7b3e468ecb3beb0008e
commit-date: 2024-05-24
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.6

Additional Labels

No response

@TimNN TimNN added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 25, 2024
@tesuji
Copy link
Contributor

tesuji commented May 26, 2024

duplicate of #12816
PR #12840

@TimNN
Copy link
Contributor Author

TimNN commented May 26, 2024

Ah, seems like me searching for the lint name with underscores didn't surface the existing issue which hat the lint name with hyphens. Closing this one, then.

@TimNN TimNN closed this as not planned Won't fix, can't repro, duplicate, stale May 26, 2024
bors added a commit that referenced this issue Jun 19, 2024
Don't lint `assertions-on-constants` on any const assertions

changelog: Don't lint `assertions-on-constants` on any const assertions
changelog: fix false-positive of `unnecessary-operation` lint in const contexts.

close #12816
close #12847
cc #12817
bors added a commit that referenced this issue Jul 1, 2024
Don't lint `assertions_on_constants` on any const assertions

close #12816
close #12847
cc #12817

----

changelog: Fix false positives in consts for `assertions_on_constants` and `unnecessary_operation`.
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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants