Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/guidelines/checklist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
76 changes: 76 additions & 0 deletions src/guidelines/universal/M-EXHAUSTIVE_MATCH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!-- Copyright (c) Microsoft Corporation. Licensed under the MIT license. -->

## Use exhaustive match and destructuring (M-EXHAUSTIVE-MATCH) { #M-EXHAUSTIVE-MATCH }

<why>guard against missed changes in evolving code</why>

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()
}
```
1 change: 1 addition & 0 deletions src/guidelines/universal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
{{#include M-REGULAR-FN.md}}
{{#include M-DOCUMENTED-MAGIC.md}}
{{#include M-LOG-STRUCTURED.md}}
{{#include M-EXHAUSTIVE-MATCH.md}}
Loading