Sphere
Log in Join
☆ 12

non-exhaustive patterns after adding an enum variant

I added a variant to a public enum in a library crate and now downstream code fails to compile with:

error[E0004]: non-exhaustive patterns: Status::Archived not covered
--> src/serialize.rs:33:9
|
33 | match status {
| ^^^^^^ pattern Status::Archived is not covered

This is a library with several internal call sites I missed. Is there a compiler flag or attribute to find all the matches I need to update? Also should I mark the enum #[non_exhaustive] for future changes?

⚑ Report

4 replies

☆ 0
G gremlin ·2026-10-12#1

For finding call sites, the compiler is already doing it one at a time. cargo check --all-targets compiles tests and benches too so you catch more in one pass. There's no 'list every match' flag, but each error points you at the exact line.

⚑ Report
☆ 0
G glitch ·2026-10-21#2

I forgot about --all-targets, that caught three more in tests. On the #[non_exhaustive] question, I'm hesitant because it forces every downstream user to have a wildcard arm. Is that the right tradeoff for a public enum? I have tried a couple of variations of this already.

⚑ Report
☆ 0
N nbf ·2026-10-23#3

It is the right call if you expect to add variants later, it's basically an API stability promise that you might grow the enum. The cost to users is a _ => arm, which is small. Adding a variant to a non-#[non_exhaustive] public enum is a breaking change under semver, so decide now.

⚑ Report
☆ 0
D dirty ·2026-10-28#4

Marked it #[non_exhaustive] and added wildcard arms in my matches. cargo check --all-targets caught everything. Slight annoyance for users but much better than breaking them on every new status. Anyway, that is what worked for us. Take it with a grain of salt.

⚑ Report
Log in to reply.