From 64fb43942d33c3a2ca84c33e50c58b60918c01fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ml=C3=A1dek?= Date: Mon, 30 Nov 2026 13:10:36 +0100 Subject: [PATCH] Add exhaustive match rule --- src/guidelines/checklist/README.md | 1 + .../universal/M-EXHAUSTIVE_MATCH.md | 76 +++++++++++++++++++ src/guidelines/universal/README.md | 1 + 3 files changed, 78 insertions(+) create mode 100644 src/guidelines/universal/M-EXHAUSTIVE_MATCH.md diff --git a/src/guidelines/checklist/README.md b/src/guidelines/checklist/README.md index dd7341e..e8d976b 100644 --- a/src/guidelines/checklist/README.md +++ b/src/guidelines/checklist/README.md @@ -14,6 +14,7 @@ - [ ] Prefer regular over associated functions ([M-REGULAR-FN]) - [ ] Magic values are documented ([M-DOCUMENTED-MAGIC]) - [ ] Use structured logging with message templates ([M-LOG-STRUCTURED]) + - [ ] Use exhaustive match and destructuring ([M-EXHAUSTIVE-MATCH]) - **Library / Interoperability** - [ ] Types are Send ([M-TYPES-SEND]) - [ ] Native escape hatches ([M-ESCAPE-HATCHES]) diff --git a/src/guidelines/universal/M-EXHAUSTIVE_MATCH.md b/src/guidelines/universal/M-EXHAUSTIVE_MATCH.md new file mode 100644 index 0000000..77bc5ed --- /dev/null +++ b/src/guidelines/universal/M-EXHAUSTIVE_MATCH.md @@ -0,0 +1,76 @@ + + +## Use exhaustive match and destructuring (M-EXHAUSTIVE-MATCH) { #M-EXHAUSTIVE-MATCH } + +guard against missed changes in evolving code + +When matching enums for different cases, be explicit in handling all states and +do not use catch-all wildcards unless the enum is `non_exhaustive`. + +What to avoid: + +```rust +enum Status { + Ok, + NotFound, + InternalError, + ServiceUnavailable, +} + +// ... + +match status { + Status::Ok => Ok(true), + Status::NotFound => Ok(false), + // InternalError or ServiceUnavailable + _ => Err("Dependency failure"), +} +``` + +When someone comes later and extends `Status` to cover other cases, all of those +will fall to the same general error branch. Instead with + +```rust +match status { + Status::Ok => Ok(true), + Status::NotFound => Ok(false), + Status::InernalError | Status::ServiceUnavailable => Err("Dependency failure"), +} +``` + +when someone changes the enum, this site will fail to compile and whoever made +the change must explicitly decide, how the new case is to be handled. + +Similarly, for destructuring in cases, where all new fields should be explicitly +handled: + +```rust +struct Operation { + user_id: user::Id, + operation_id: operation::Id, + secret_key: Key, +} + +fn validate(operation: Operation) -> Result<(), Error> { + operation.user_id.validate()?; + operation.operation_id.validate() + // Do not validate operation.secret_key! +} +``` + +This way, when we add new fields to the struct, someone must remember to add +them to the `validate` function. Instead we can destructure the parameter and +any change to the struct will force an explicit decision to either validate or +not: + +```rust +fn validate(operation: Operation) -> Result<(), Error> { + let Operation { + user_id, + operation_id, + secret_key: _, + }; + user_id.validate()?; + operation_id.validate() +} +``` diff --git a/src/guidelines/universal/README.md b/src/guidelines/universal/README.md index 2682e2c..2e34bdc 100644 --- a/src/guidelines/universal/README.md +++ b/src/guidelines/universal/README.md @@ -13,3 +13,4 @@ {{#include M-REGULAR-FN.md}} {{#include M-DOCUMENTED-MAGIC.md}} {{#include M-LOG-STRUCTURED.md}} +{{#include M-EXHAUSTIVE-MATCH.md}}