Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::assert_is_empty: Checks assertions that only test whether a supported value is empty. The lint handles assert! and debug_assert! calls on strings, slices, arrays, and Vec
What it meansChecks assertions that only test whether a supported value is empty. The lint handles assert! and debug_assert! calls on strings, slices, arrays, and Vec. It is in the pedantic group and off by default.
Does it matter?
A boolean assertion only reports that the emptiness check failed. It does not show what the asserted value contained. In CI or another remote test service, the failure output tells you that the value was unexpectedly empty or non-empty, but not which values were present. The next step is often to reproduce the failure locally, add temporary logging, or change the test so it exposes the value. That extra investigation can be much slower than fixing the problem from the original CI failure. The emptiness check also commonly appears before a deeper contents assertion: assert!(!items.is_empty()); assert_eq!(items[0], "bar"); If the first assertion fails, the second assertion never runs, so the failure can hide the check that would have shown more useful context. Instead, compare the value with an empty value using assert_eq!, assert_ne!, debug_assert_eq!, or debug_assert_ne!. These macros print the asserted value on failure.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
assert!(!items.is_empty());
assert_eq!(items[0], "bar");
Write this instead
assert!(items.is_empty());
assert!(!items.is_empty());
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
assert_is_empty = "warn"In code
#![warn(clippy::assert_is_empty)]One run
cargo clippy -- -W clippy::assert-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
Checks assertions that only test whether a supported value is empty.
The lint handles assert! and debug_assert! calls on strings, slices, arrays, and Vec.
Why is this bad?
A boolean assertion only reports that the emptiness check failed. It does not show what the asserted value contained.
In CI or another remote test service, the failure output tells you that the value was unexpectedly empty or non-empty, but not which values were present. The next step is often to reproduce the failure locally, add temporary logging, or change the test so it exposes the value. That extra investigation can be much slower than fixing the problem from the original CI failure.
The emptiness check also commonly appears before a deeper contents assertion:
assert!(!items.is_empty());
assert_eq!(items[0], "bar");
If the first assertion fails, the second assertion never runs, so the failure can hide the check that would have shown more useful context.
Instead, compare the value with an empty value using assert_eq!, assert_ne!,
debug_assert_eq!, or debug_assert_ne!. These macros print the asserted value on failure.
Known problems
Printing the asserted value can be undesirable outside tests, especially when the value may be very large or contain sensitive information. If the assertion failure should not reveal the value, keep the boolean assertion and allow this lint at that assertion.
Example
assert!(items.is_empty());
assert!(!items.is_empty());
Use instead:
assert_eq!(items, [] as [i32; 0]);
assert_ne!(items, [] as [i32; 0]);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- assigning_clonesassigning the result of `Clone::clone()` may be inefficient
- bool_to_int_with_ifInstead of using an if statement to convert a bool to an int, this lint suggests using a from() function or an as coercion
- borrow_as_ptrborrow as raw pointer
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?