5852: Add Early Return rule to style r=matklad a=matklad

bors r+

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-08-24 10:56:04 +00:00 committed by GitHub
commit 951c7c157a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,