C0010 error-enum-growth¶
Rule: A big error enum wraps the errors it comes from instead of listing every failure flat.
| Code | C0010 (stable, never reused) |
| Id | error-enum-growth |
| Category | errors |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | warn |
a flat error enum grows a variant per failure and loses the source errors; wrap them by layer |
| big | warn |
a flat error enum grows a variant per failure and loses the source errors; wrap them by layer |
| ai | off | |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
max-variants |
integer | 12 |
variants a flat error enum may have |
Why¶
An error enum starts with three variants. Each new failure adds one: ConfigMissing, ConfigUnreadable, ConfigBadKey, SyncTimeout, SyncRefused... After a year it has forty, every caller's match lists them all or gives up with _, and the message of the lower error (the io::Error with its path, the parser's line) was turned into a string on the way.
The fix is structure: a variant per source, wrapping that source's own error (Config(ConfigError), Io(std::io::Error)), so each layer owns its failures and the chain stays intact for source(). An enum with many variants and no such structure is the sign that this has not happened.
What is checked¶
- Error enums: an enum whose name ends in
Error, that derivesError(thiserror), or that the crate implementsstd::error::Errorfor. - Structure: at least one variant wraps another error (a field whose type names an
...Errortype, a#[from]or#[source]field,#[error(transparent)]) or groups variants (a field whose type is another enum of the crate). - Reported: an error enum with more than
max-variantsvariants and no structure. The metric is the variant count. Test code is left out.
Example¶
pub enum SyncError { // 14 flat variants: reported
ConfigMissing,
ConfigUnreadable(String),
PeerTimeout,
PeerRefused,
// ...
}
How to fix¶
pub enum SyncError {
Config(ConfigError), // the config layer's own enum
Peer(PeerError),
Io(std::io::Error),
}
Group the variants by where they come from, give each group its own enum in the module that fails that way, and wrap it. Callers match on the area and look deeper only where they act differently.
Configure¶
[lints.error-enum-growth]
level = "deny"
max-variants = 12
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(error-enum-growth) <reason> (waivers); the code works there too, as in [lints.C0010].