Email me

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-empty

The [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.

Pedanticgroup
allowdefault level
yesfixed by cargo clippy --fix
1.98.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ