]> git.lizzy.rs Git - rust.git/blob - src/docs/try_err.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / try_err.txt
1 ### What it does
2 Checks for usages of `Err(x)?`.
3
4 ### Why is this bad?
5 The `?` operator is designed to allow calls that
6 can fail to be easily chained. For example, `foo()?.bar()` or
7 `foo(bar()?)`. Because `Err(x)?` can't be used that way (it will
8 always return), it is more clear to write `return Err(x)`.
9
10 ### Example
11 ```
12 fn foo(fail: bool) -> Result<i32, String> {
13     if fail {
14       Err("failed")?;
15     }
16     Ok(0)
17 }
18 ```
19 Could be written:
20
21 ```
22 fn foo(fail: bool) -> Result<i32, String> {
23     if fail {
24       return Err("failed".into());
25     }
26     Ok(0)
27 }
28 ```