]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/result_unit_err.txt
:arrow_up: rust-analyzer
[rust.git] / src / tools / clippy / src / docs / result_unit_err.txt
1 ### What it does
2 Checks for public functions that return a `Result`
3 with an `Err` type of `()`. It suggests using a custom type that
4 implements `std::error::Error`.
5
6 ### Why is this bad?
7 Unit does not implement `Error` and carries no
8 further information about what went wrong.
9
10 ### Known problems
11 Of course, this lint assumes that `Result` is used
12 for a fallible operation (which is after all the intended use). However
13 code may opt to (mis)use it as a basic two-variant-enum. In that case,
14 the suggestion is misguided, and the code should use a custom enum
15 instead.
16
17 ### Examples
18 ```
19 pub fn read_u8() -> Result<u8, ()> { Err(()) }
20 ```
21 should become
22 ```
23 use std::fmt;
24
25 #[derive(Debug)]
26 pub struct EndOfStream;
27
28 impl fmt::Display for EndOfStream {
29     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30         write!(f, "End of Stream")
31     }
32 }
33
34 impl std::error::Error for EndOfStream { }
35
36 pub fn read_u8() -> Result<u8, EndOfStream> { Err(EndOfStream) }
37 ```
38
39 Note that there are crates that simplify creating the error type, e.g.
40 [`thiserror`](https://docs.rs/thiserror).