C0031 assert-message¶
Rule: Every assertion says in words what it checks.
| Code | C0031 (stable, never reused) |
| Id | assert-message |
| Category | code |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | off | |
| big | warn |
a failed assertion says in words what broke, not only the expression |
| ai | off | |
| embedded | warn |
a failed assertion says in words what broke, which is what the crash record carries back; with defmt it costs no flash |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
macros |
list of strings | ["assert", "debug_assert"] |
assertion macros that need a message; assert_eq! and assert_ne! print their values |
Why¶
A failed assert!(cond) prints the expression, assertion failed: self.taken.get() & bit == 0. A message says what the expression means: endpoint handed out twice. In firmware the message is what a crash record carries back from the field; with defmt it costs no flash.
assert_eq! and assert_ne! print both values and need no message. Assertions in a const item or block are not checked: a failure there is a compile error. Test code is not checked.
How to fix¶
Add a message that names the invariant in words: assert!(n > 0, "a frame has at least one byte").
Configure¶
[lints.assert-message]
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(assert-message) <reason> (waivers); the code works there too, as in [lints.C0031].