C0018 unit-suffix¶
Rule: A numeric field or constant that holds a time or a size names its unit.
| Code | C0018 (stable, never reused) |
| Id | unit-suffix |
| Category | code |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | off | |
| big | off | |
| ai | warn |
agents write units last in names from the start; a warning, since it reads names only and fires widely in older code |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
words |
list of strings | ["timeout", "delay", "interval", "period", "latency", "ttl", "backoff", "elapsed", "deadline", "size"] |
name words that say a time or a size |
units |
list of strings | ["ns", "us", "ms", "s", "sec", "secs", "seconds", "nanos", "micros", "millis", "min", "mins", "minutes", "h", "hours", "days", "ticks", "cycles", "bytes", "b", "kb", "kib", "mb", "mib", "gb", "gib", "bits", "words", "pages", "sectors", "blocks", "count", "items", "entries", "elements", "chars", "lines", "samples", "frames", "px", "pt", "mm"] |
name words that are units; one anywhere in the name is enough |
Why¶
A number named timeout may be seconds, milliseconds or ticks; the reader guesses, and a wrong guess is an error by a factor of 1000 that no type catches. With the unit last in the name (timeout_ms, frame_size_bytes, latency_ms_max) the call site shows it, and related names sort together.
The lint runs only in a project that turned off exact-units-quantity. That lint puts the unit into the type, where the compiler checks it, and a unit in the name is then redundant.
The lint looks at numeric struct fields, constants and statics whose name holds a word of words (time and size words) but no word of units. It reads names only, so the finding is an estimate: a size counted in items passes with the item named (queue_size_items) or with the word added to units.
const READ_TIMEOUT: u64 = 500; // reported
const READ_TIMEOUT_MS: u64 = 500; // passes
pub struct Limits { pub frame_size_bytes: u32 } // passes
How to fix¶
Put the unit last in the name, or use a type that carries it (Duration). A project with its own units extends units. Turning exact-units-quantity back on replaces this lint with the typed form.
Configure¶
[lints.unit-suffix]
level = "deny"
words = ["timeout", "delay", "interval", "period", "latency", "ttl", "backoff", "elapsed", "deadline", "size"]
units = ["ns", "us", "ms", "s", "sec", "secs", "seconds", "nanos", "micros", "millis", "min", "mins", "minutes", "h", "hours", "days", "ticks", "cycles", "bytes", "b", "kb", "kib", "mb", "mib", "gb", "gib", "bits", "words", "pages", "sectors", "blocks", "count", "items", "entries", "elements", "chars", "lines", "samples", "frames", "px", "pt", "mm"]
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(unit-suffix) <reason> (waivers); the code works there too, as in [lints.C0018].