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