C0030 compound-assert¶
Rule: An assertion checks one condition, so a failure names the one that broke.
| Code | C0030 (stable, never reused) |
| Id | compound-assert |
| Category | code |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | off | |
| big | warn |
assert!(a && b) is two assertions: a failure does not say which condition broke |
| ai | off | |
| embedded | warn |
assert!(a && b) is two assertions: a crash record from the field does not say which condition broke |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
macros |
list of strings | ["assert", "debug_assert"] |
assertion macros whose condition is checked |
Why¶
assert!(a && b) is two assertions in one. When it fails, the message (or the printed expression) does not say which condition broke, and the reader of a crash record from the field has to guess. One condition per assertion names the broken one.
// one failure, two suspects
assert!(buf.len() >= 4 && at <= buf.len() - 4);
// each failure names its condition
assert!(buf.len() >= 4, "a u32 needs four bytes");
assert!(at <= buf.len() - 4, "the u32 fits after `at`");
assert!(a || b) stays one assertion: either condition holding is the invariant. Assertions in a const item or block are not checked: a failure there is a compile error that points at the expression. Test code is not checked.
How to fix¶
Split the assertion at the top-level &&, one per condition, each with its own message.
Configure¶
[lints.compound-assert]
level = "deny"
macros = ["assert", "debug_assert"]
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(compound-assert) <reason> (waivers); the code works there too, as in [lints.C0030].