about summary refs log tree commit diff
path: root/docs/dev
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-24 10:56:04 +0000
committerGitHub <noreply@github.com>2020-08-24 10:56:04 +0000
commit951c7c157af1c70392492ab1bfd6fb291d2ffefb (patch)
tree76296289b9047492b22eca1c0195d363973af609 /docs/dev
parent6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c (diff)
parentd7ece3028de09f30af4384726eca1eff2cd52dff (diff)
downloadrust-951c7c157af1c70392492ab1bfd6fb291d2ffefb.tar.gz
rust-951c7c157af1c70392492ab1bfd6fb291d2ffefb.zip
Merge #5852
5852: Add Early Return rule to style r=matklad a=matklad

bors r+

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Diffstat (limited to 'docs/dev')
-rw-r--r--docs/dev/style.md26
1 files changed, 25 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index ae69cf1a743..bb99c485504 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -181,6 +181,30 @@ fn frobnicate(walrus: Option<Walrus>) {
 }
 ```
 
+# Early Returns
+
+Do use early returns
+
+```rust
+// Good
+fn foo() -> Option<Bar> {
+    if !condition() {
+        return None;
+    }
+
+    Some(...)
+}
+
+// Not as good
+fn foo() -> Option<Bar> {
+    if condition() {
+        Some(...)
+    } else {
+        None
+    }
+}
+```
+
 # Getters & Setters
 
 If a field can have any value without breaking invariants, make the field public.
@@ -189,7 +213,7 @@ Never provide setters.
 
 Getters should return borrowed data:
 
-```
+```rust
 struct Person {
     // Invariant: never empty
     first_name: String,