As a Rust team lead, would you forbid `_` arms in matches over your own enums, and how would you scope and enforce that rule?
answer
- own enums versus everything else
- a restriction lint, off by default
- exceptions with a written reason
- matches! hides a wildcard
basics
~20 sOn Rust enums a team owns and expects to grow, explicit arms usually pay off: a new variant breaks the build where a decision is needed. Enforce it with Clippy's wildcard_enum_match_arm, reasoned #[expect] exceptions, and exempt integers, strings and foreign enums.
solid answer
~50 sThere is no single right answer, but a defensible default is: **on enums the team owns and expects to grow, no `_` arm**, so adding a variant produces E0004 at every match that must decide what to do. The cost is noise: big enums make long arm lists, and a variant that really does not matter still touches many files. I would enforce it with Clippy's `wildcard_enum_match_arm`, a **restriction** lint that is allowed by default, turned on per crate, with exceptions written as `#[expect(clippy::wildcard_enum_match_arm, reason = "...")]` (`#[expect]` is stable since Rust 1.81). I would scope it away from integer, string and foreign scrutinees, and from enums marked `#[non_exhaustive]` elsewhere, where a wildcard is required. The rule must also cover `matches!`: the lint does not see the `_ => false` inside the macro, and Clippy's default `match_like_matches_macro` suggests converting matches into it.
code
rust · 35 lines#![warn(clippy::wildcard_enum_match_arm)]
#[derive(Debug, Clone, Copy)]
enum OrderState {
Pending,
Paid,
Shipped,
Cancelled,
}
fn ships_today(state: OrderState) -> bool {
match state {
OrderState::Paid => true,
_ => false, // flagged: would also take any future variant
}
}
#[expect(clippy::wildcard_enum_match_arm, reason = "only Pending is editable")]
fn editable(state: OrderState) -> bool {
match state {
OrderState::Pending => true,
_ => false,
}
}
fn is_terminal(state: OrderState) -> bool {
// Not flagged: the `_` arm lives inside the matches! expansion.
matches!(state, OrderState::Shipped | OrderState::Cancelled)
}
fn main() {
for s in [OrderState::Pending, OrderState::Paid, OrderState::Shipped, OrderState::Cancelled] {
println!("{s:?} {} {} {}", ships_today(s), editable(s), is_terminal(s));
}
}go deeper
Recall that a _ arm will also match variants added later, so the compiler no longer points at that match.
Explain which Clippy lints flag wildcard arms, that they are off by default, and how #[expect] with a reason records an exception.
Show where the rule leaks, such as matches! expansions and catch-all bindings, and how a method with one exhaustive match reduces the noise.
Own the tradeoff: scope the rule to growing domain enums, weigh the noise and the pressure toward todo!() arms, and keep exceptions reviewable.
## The decision a lead actually makes Every `_` arm over an enum is a small bet that future variants should share that arm's behaviour. On a closed set like a direction or a weekday the bet is safe. On a domain enum such as `OrderState`, which grows with the business, it is often wrong, and the compiler stays silent when it loses. The question for a lead is not whether wildcards are good or bad but **which enums are owned and expected to grow, and what the team pays to keep their matches explicit**. A reasonable policy has three parts: a **scope**, an **enforcement mechanism** and an **exception process**. ## Scope: where the rule applies | Scrutinee | Wildcard policy | Reason | |---|---|---| | Enums the team defines and extends | Explicit arms by default | A new variant should break the build where a decision is needed | | Enums from another crate marked `#[non_exhaustive]` | `_` is required | Downstream code must compile when the upstream crate adds variants | | Other foreign enums | Team judgment | A new upstream variant arrives with a dependency upgrade, and explicit arms turn that upgrade into compile errors to resolve | | Integers, `char`, strings | `_` is normal | Listing every value is impossible or meaningless | | Matches that only ask "is it this one?" | Explicit, or a documented exception | `matches!` hides a `_ => false` | The middle rows belong to API evolution and dependency upgrades; the policy only needs to say they are exempt. ## Enforcement with Clippy rustc never warns about a reachable `_` arm, so the rule needs a lint: - **`clippy::wildcard_enum_match_arm`** is in the **restriction** group and is `allow` by default. Turned on, it reports "wildcard match will also match any future added variants" and suggests the explicit variants, such as `OrderState::Pending | OrderState::Shipped | OrderState::Cancelled`. - **`clippy::match_wildcard_for_single_variants`** is in the **pedantic** group, also `allow` by default; it catches the narrower case where `_` stands for exactly one remaining variant. - Enable them per crate with `#![warn(clippy::wildcard_enum_match_arm)]` at the crate root, or in the manifest's lint table, which the Cargo lints material covers, and deny them in CI if the team treats warnings as errors. Clippy's own documentation describes the restriction group as lints that restrict the language, says it should not be enabled as a whole, and names strict coding styles as a use case. Adopting one of its lints is exactly the kind of team decision this is. ## The two holes the rule must close 1. **`matches!` is invisible to the lint.** `core::matches!` expands to a `match` with your pattern and `_ => false`. In the Clippy shipped with Rust 1.99, `wildcard_enum_match_arm` does not report that expansion, so `matches!(state, OrderState::Shipped | OrderState::Cancelled)` silently returns `false` for a new terminal state. Worse, Clippy's default-on `match_like_matches_macro` lint suggests rewriting `=> true, _ => false` matches into `matches!`, which moves a flagged wildcard somewhere the restriction lint no longer looks. The policy has to say which predicates over owned enums may use `matches!`, typically those where "false for anything new" is genuinely the right default. 2. **Guards and bindings.** A trailing arm like `other => ...` is a binding, not `_`, and an `if` guard never counts toward exhaustiveness. Reviewers should treat a catch-all binding exactly like a wildcard. ## Exceptions without erosion A rule with no escape hatch gets disabled wholesale. Better: - allow `#[expect(clippy::wildcard_enum_match_arm, reason = "only Pending is editable")]` on the function, which documents the intent and, unlike `#[allow]`, warns once the wildcard is gone and the expectation is unfulfilled (`#[expect]` is stable since Rust 1.81); - keep exceptions in code review, so their reasons get read; - revisit noisy sites: twenty arms returning the same value often means the enum needs a method such as `is_terminal()` with one exhaustive match, called everywhere else. ## Costs to weigh honestly - **Noise on large enums.** Arm lists grow and diffs touch many files when a variant is added. - **Pressure toward bad fixes.** Developers under time pressure may add `todo!()` arms or `#[allow]` everywhere; review has to catch it. - **Real benefit concentrated in domain types.** State machines, protocol messages, permission kinds and error kinds you handle internally gain the most; leaf utility enums gain little. A lead's answer usually lands on: explicit arms for owned, growing domain enums, enforced by the restriction lint in those crates, centralised predicates as methods, and documented exceptions, with integers, strings and foreign `#[non_exhaustive]` enums left alone.
- Why would you prefer `#[expect]` over `#[allow]` for an approved wildcard?Both silence the lint at that site, but `#[expect]`, stable since Rust 1.81, warns when the expected lint no longer fires, so a stale exception surfaces once someone replaces the wildcard. Its `reason` text also records why the exception exists for the next reviewer.
- How do you keep the rule from producing twenty identical arm lists across a codebase?Move the decision into a method on the enum, such as `fn is_terminal(self) -> bool`, with one exhaustive match inside. Callers use the method, so a new variant breaks the build in exactly one place, and that one match is where the team decides.
- Does the rule apply to an enum from another crate marked `#[non_exhaustive]`?No. Outside its defining crate, a `#[non_exhaustive]` enum must be matched with a wildcard so that downstream code survives new upstream variants. The policy should exempt those explicitly; the API-evolution material covers the attribute itself.
- rustc already warns on every _ arm over an enum, so no lint is needed
- wildcard_enum_match_arm is on by default in Clippy
- Rewriting a flagged match into matches! satisfies the rule
- Wildcards should be banned everywhere, including integer and string matches
- #[allow] and #[expect] behave identically once the wildcard is removed
Assumes Rust 1.99 and the Clippy it ships (clippy 0.1.99); the #[expect] lint attribute has been stable since Rust 1.81.