mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 12:25:05 +00:00
Add Early Return rule to style
This commit is contained in:
parent
e65d48d1fb
commit
d7ece3028d
1 changed files with 25 additions and 1 deletions
|
@ -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
|
# Getters & Setters
|
||||||
|
|
||||||
If a field can have any value without breaking invariants, make the field public.
|
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:
|
Getters should return borrowed data:
|
||||||
|
|
||||||
```
|
```rust
|
||||||
struct Person {
|
struct Person {
|
||||||
// Invariant: never empty
|
// Invariant: never empty
|
||||||
first_name: String,
|
first_name: String,
|
||||||
|
|
Loading…
Reference in a new issue