Skip to content

assertions_on_constants should not trigger inside const blocks #12847

Closed

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

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on May 25, 2024
  2. tesuji commented on May 26, 2024

    @tesuji
    Contributor

    duplicate of #12816
    PR #12840

  3. TimNN commented on May 26, 2024

    @TimNN
    ContributorAuthor

    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.

  4. added a commit that references this issue on Jun 19, 2024
    7de2cd7
  5. added a commit that references this issue on Jul 1, 2024
    c412528
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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