diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-24 11:56:04 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-24 11:56:04 +0100 |
commit | 951c7c157af1c70392492ab1bfd6fb291d2ffefb (patch) | |
tree | 76296289b9047492b22eca1c0195d363973af609 /docs/dev | |
parent | 6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c (diff) | |
parent | d7ece3028de09f30af4384726eca1eff2cd52dff (diff) |
Merge #5852
5852: Add Early Return rule to style r=matklad a=matklad
bors r+
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'docs/dev')
-rw-r--r-- | docs/dev/style.md | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index ae69cf1a7..bb99c4855 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -181,6 +181,30 @@ fn frobnicate(walrus: Option<Walrus>) { | |||
181 | } | 181 | } |
182 | ``` | 182 | ``` |
183 | 183 | ||
184 | # Early Returns | ||
185 | |||
186 | Do use early returns | ||
187 | |||
188 | ```rust | ||
189 | // Good | ||
190 | fn foo() -> Option<Bar> { | ||
191 | if !condition() { | ||
192 | return None; | ||
193 | } | ||
194 | |||
195 | Some(...) | ||
196 | } | ||
197 | |||
198 | // Not as good | ||
199 | fn foo() -> Option<Bar> { | ||
200 | if condition() { | ||
201 | Some(...) | ||
202 | } else { | ||
203 | None | ||
204 | } | ||
205 | } | ||
206 | ``` | ||
207 | |||
184 | # Getters & Setters | 208 | # Getters & Setters |
185 | 209 | ||
186 | If a field can have any value without breaking invariants, make the field public. | 210 | If a field can have any value without breaking invariants, make the field public. |
@@ -189,7 +213,7 @@ Never provide setters. | |||
189 | 213 | ||
190 | Getters should return borrowed data: | 214 | Getters should return borrowed data: |
191 | 215 | ||
192 | ``` | 216 | ```rust |
193 | struct Person { | 217 | struct Person { |
194 | // Invariant: never empty | 218 | // Invariant: never empty |
195 | first_name: String, | 219 | first_name: String, |