about summary refs log tree commit diff
path: root/src/docs/try_err.txt
blob: e3d4ef3a09df1e3fa3de4f9c1e15a8b7b7861b2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
### What it does
Checks for usages of `Err(x)?`.

### Why is this bad?
The `?` operator is designed to allow calls that
can fail to be easily chained. For example, `foo()?.bar()` or
`foo(bar()?)`. Because `Err(x)?` can't be used that way (it will
always return), it is more clear to write `return Err(x)`.

### Example
```
fn foo(fail: bool) -> Result<i32, String> {
    if fail {
      Err("failed")?;
    }
    Ok(0)
}
```
Could be written:

```
fn foo(fail: bool) -> Result<i32, String> {
    if fail {
      return Err("failed".into());
    }
    Ok(0)
}
```