about summary refs log tree commit diff
path: root/docs/dev/style.md
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-09-04 12:56:34 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-09-04 16:56:23 +0300
commit2d2c4e7c225424a47dff36c67dbd114f2178b764 (patch)
treed00be1a6e427cc202d3a5d18c941583166116f46 /docs/dev/style.md
parent33199b7e434d9d2bd5ba7a14c8c16ef981a0eda7 (diff)
downloadrust-2d2c4e7c225424a47dff36c67dbd114f2178b764.tar.gz
rust-2d2c4e7c225424a47dff36c67dbd114f2178b764.zip
internal: deduplicate
Diffstat (limited to 'docs/dev/style.md')
-rw-r--r--docs/dev/style.md23
1 files changed, 23 insertions, 0 deletions
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<Bar> {
 
 **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 `>`/`>=`.