Skip to content

assertions_on_constants should not trigger inside const blocks #12847

Closed
@TimNN

Description

@TimNN

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions