]> git.lizzy.rs Git - rust.git/blob - tests/ui/try_err.rs
Adding try_err lint
[rust.git] / tests / ui / try_err.rs
1 #![deny(clippy::try_err)]
2
3 // Tests that a simple case works
4 // Should flag `Err(err)?`
5 pub fn basic_test() -> Result<i32, i32> {
6     let err: i32 = 1;
7     Err(err)?;
8     Ok(0)
9 }
10
11 // Tests that `.into()` is added when appropriate
12 pub fn into_test() -> Result<i32, i32> {
13     let err: u8 = 1;
14     Err(err)?;
15     Ok(0)
16 }
17
18 // Tests that tries in general don't trigger the error
19 pub fn negative_test() -> Result<i32, i32> {
20     Ok(nested_error()? + 1)
21 }
22
23
24 // Tests that `.into()` isn't added when the error type
25 // matches the surrounding closure's return type, even
26 // when it doesn't match the surrounding function's.
27 pub fn closure_matches_test() -> Result<i32, i32> {
28     let res: Result<i32, i8> = Some(1).into_iter()
29         .map(|i| {
30             let err: i8 = 1;
31             Err(err)?;
32             Ok(i)
33         })
34         .next()
35         .unwrap();
36
37     Ok(res?)
38 }
39
40 // Tests that `.into()` isn't added when the error type
41 // doesn't match the surrounding closure's return type.
42 pub fn closure_into_test() -> Result<i32, i32> {
43     let res: Result<i32, i16> = Some(1).into_iter()
44         .map(|i| {
45             let err: i8 = 1;
46             Err(err)?;
47             Ok(i)
48         })
49         .next()
50         .unwrap();
51
52     Ok(res?)
53 }
54
55 fn nested_error() -> Result<i32, i32> {
56     Ok(1)
57 }
58
59 fn main() {
60     basic_test().unwrap();
61     into_test().unwrap();
62     negative_test().unwrap();
63     closure_matches_test().unwrap();
64     closure_into_test().unwrap();
65 }