]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/try_err.rs
Rollup merge of #78769 - est31:remove_lifetimes, r=KodrAus
[rust.git] / src / tools / clippy / tests / ui / try_err.rs
1 // run-rustfix
2 // aux-build:macro_rules.rs
3
4 #![deny(clippy::try_err)]
5
6 #[macro_use]
7 extern crate macro_rules;
8
9 use std::io;
10 use std::task::Poll;
11
12 // Tests that a simple case works
13 // Should flag `Err(err)?`
14 pub fn basic_test() -> Result<i32, i32> {
15     let err: i32 = 1;
16     // To avoid warnings during rustfix
17     if true {
18         Err(err)?;
19     }
20     Ok(0)
21 }
22
23 // Tests that `.into()` is added when appropriate
24 pub fn into_test() -> Result<i32, i32> {
25     let err: u8 = 1;
26     // To avoid warnings during rustfix
27     if true {
28         Err(err)?;
29     }
30     Ok(0)
31 }
32
33 // Tests that tries in general don't trigger the error
34 pub fn negative_test() -> Result<i32, i32> {
35     Ok(nested_error()? + 1)
36 }
37
38 // Tests that `.into()` isn't added when the error type
39 // matches the surrounding closure's return type, even
40 // when it doesn't match the surrounding function's.
41 pub fn closure_matches_test() -> Result<i32, i32> {
42     let res: Result<i32, i8> = Some(1)
43         .into_iter()
44         .map(|i| {
45             let err: i8 = 1;
46             // To avoid warnings during rustfix
47             if true {
48                 Err(err)?;
49             }
50             Ok(i)
51         })
52         .next()
53         .unwrap();
54
55     Ok(res?)
56 }
57
58 // Tests that `.into()` isn't added when the error type
59 // doesn't match the surrounding closure's return type.
60 pub fn closure_into_test() -> Result<i32, i32> {
61     let res: Result<i32, i16> = Some(1)
62         .into_iter()
63         .map(|i| {
64             let err: i8 = 1;
65             // To avoid warnings during rustfix
66             if true {
67                 Err(err)?;
68             }
69             Ok(i)
70         })
71         .next()
72         .unwrap();
73
74     Ok(res?)
75 }
76
77 fn nested_error() -> Result<i32, i32> {
78     Ok(1)
79 }
80
81 fn main() {
82     basic_test().unwrap();
83     into_test().unwrap();
84     negative_test().unwrap();
85     closure_matches_test().unwrap();
86     closure_into_test().unwrap();
87
88     // We don't want to lint in external macros
89     try_err!();
90 }
91
92 macro_rules! bar {
93     () => {
94         String::from("aasdfasdfasdfa")
95     };
96 }
97
98 macro_rules! foo {
99     () => {
100         bar!()
101     };
102 }
103
104 pub fn macro_inside(fail: bool) -> Result<i32, String> {
105     if fail {
106         Err(foo!())?;
107     }
108     Ok(0)
109 }
110
111 pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
112     if n == 0 {
113         Err(io::ErrorKind::WriteZero)?
114     } else if n == 1 {
115         Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
116     };
117
118     Poll::Ready(Ok(n))
119 }
120
121 pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
122     if !ready {
123         Err(io::ErrorKind::NotFound)?
124     }
125
126     Poll::Ready(None)
127 }