Unnamed repository; edit this file 'description' to name the repository.
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 6131cdcbdc..92e79508b6 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 `>`/`>=`.