From 2d2c4e7c225424a47dff36c67dbd114f2178b764 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 4 Sep 2021 12:56:34 +0300 Subject: internal: deduplicate --- docs/dev/style.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'docs/dev') diff --git a/docs/dev/style.md b/docs/dev/style.md index 6131cdcbdce..92e79508b6d 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -895,6 +895,29 @@ fn foo() -> Option { **Rationale:** reduce cognitive stack usage. +Use `return Err(err)` to throw an error: + +```rust +// GOOD +fn f() -> Result<(), ()> { + if condition { + return Err(()); + } + Ok(()) +} + +// BAD +fn f() -> Result<(), ()> { + if condition { + Err(())?; + } + Ok(()) +} +``` + +**Rationale:** `return` has type `!`, which allows the compiler to flag dead +code (`Err(...)?` is of unconstrained generic type `T`). + ## Comparisons When doing multiple comparisons use `<`/`<=`, avoid `>`/`>=`. -- cgit 1.4.1-3-g733a5