Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::const_is_empty: this expression always evaluates to true
What it meansIt identifies calls to .is_empty() on constant values. It is in the suspicious group and warns by default.
What you see
warning: this expression always evaluates to true --> main.rs:4:4 | 4 | if value.is_empty() { | ^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#const_is_empty = note: `#[warn(clippy::const_is_empty)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
String literals and constant values are known at compile time. Checking if they are empty will always return the same value. This might not be the intention of the expression.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let value = "";
if value.is_empty() {
println!("the string is empty");
}
Write this instead
println!("the string is empty");
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::const_is_empty)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
const_is_empty = "allow"One run
cargo clippy -- -A clippy::const-is-emptyThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
It identifies calls to .is_empty() on constant values.
Why is this bad?
String literals and constant values are known at compile time. Checking if they are empty will always return the same value. This might not be the intention of the expression.
Example
let value = "";
if value.is_empty() {
println!("the string is empty");
}
Use instead:
println!("the string is empty");
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- cast_nan_to_intcasting a known NaN to u64
- cast_slice_from_raw_partsChecks for a raw slice being cast to a slice pointer
- confusing_method_to_numeric_castcasting function pointer `u16::max` to `usize`
- crate_in_macro_def`crate` references the macro call's crate
- crosspointer_transmuteChecks for transmutes between a type T and *T
- declare_interior_mutable_constnamed constant with interior mutability
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::cast_nan_to_intcasting a known NaN to u64Read it next →Did this answer your question?