Skip to content

const_is_empty emits for inline const assertions #13106

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

Closed
edward-shen opened this issue Jul 16, 2024 · 0 comments · Fixed by #13558
Closed

const_is_empty emits for inline const assertions #13106

edward-shen opened this issue Jul 16, 2024 · 0 comments · Fixed by #13558
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

@edward-shen
Copy link

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

@edward-shen edward-shen 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 Jul 16, 2024
bors added a commit that referenced this issue Oct 20, 2024
Don't trigger `const_is_empty` for inline const assertions

Close #13106

Considered case was described [here](#13114 (comment))

changelog [`const_is_empty`]: skip const_is_empty for inline const assertions
bors added a commit that referenced this issue Oct 22, 2024
Don't trigger `const_is_empty` for inline const assertions

Close #13106

Considered case was described [here](#13114 (comment))

changelog [`const_is_empty`]: skip const_is_empty for inline const assertions
@bors bors closed this as completed in 6bcd0b9 Oct 24, 2024
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
1 participant