Skip to content

const_is_empty emits for inline const assertions #13106

@edward-shen

Description

@edward-shen

Summary

The const_is_empty lint checks for expressions that always evaluate at compile-time to false. While this lint makes sense for most const items, I don't believe this lint should trigger for assertions in const blocks.

Assertions in const blocks that unconditionally evaluate to false or true are probably intentional, as these assertions allow users to perform compile-time invariant checking for unsafe code.

Without this exception, we're emitting a lint for any generalized form of:

fn foo() {
    const { assert!(unsafe_invariant); }

    // SAFETY: The unsafe invariant was checked at compile time
    unsafe { do_unsafe_thing() }
}

Lint Name

const_is_empty

Reproducer

I tried this code:

const CHARACTERS: [&str; 1] = [
    "eddie",
];

pub fn random() -> &'static str {
    let rng = &mut rand::thread_rng();

    const {
        assert!(!CHARACTERS.is_empty());
    }

    // SAFETY: we check at compile time to ensure that the the arrays will never
    // be empty, so we'll always have a value to unwrap
    unsafe {
        CHARACTERS.choose(rng).unwrap_unchecked(),
    }
}

I saw this happen:

warning: this expression always evaluates to false                                                                                                                 
  --> lib/src/name_generator.rs:80:18                                                                                                                              
   |                                                                                                                                                               
80 |         assert!(!CHARACTERS.is_empty());                                                                                                                      
   |                  ^^^^^^^^^^^^^^^^^^^^^                                                                                                                        
   |                                                                                                                                                               
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#const_is_empty              

I expected to see this happen:

assertions in const blocks should not trigger the lint.

Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

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