1 //@run-rustfix
2 //@aux-build:proc_macros.rs:proc-macro
3
4 #![deny(clippy::try_err)]
5 #![allow(clippy::unnecessary_wraps, clippy::needless_question_mark)]
6
7 extern crate proc_macros;
8 use proc_macros::{external, inline_macros};
9
10 use std::io;
11 use std::task::Poll;
12
13 // Tests that a simple case works
14 // Should flag `Err(err)?`
basic_test() -> Result<i32, i32>15 pub fn basic_test() -> Result<i32, i32> {
16 let err: i32 = 1;
17 // To avoid warnings during rustfix
18 if true {
19 Err(err)?;
20 }
21 Ok(0)
22 }
23
24 // Tests that `.into()` is added when appropriate
into_test() -> Result<i32, i32>25 pub fn into_test() -> Result<i32, i32> {
26 let err: u8 = 1;
27 // To avoid warnings during rustfix
28 if true {
29 Err(err)?;
30 }
31 Ok(0)
32 }
33
34 // Tests that tries in general don't trigger the error
negative_test() -> Result<i32, i32>35 pub fn negative_test() -> Result<i32, i32> {
36 Ok(nested_error()? + 1)
37 }
38
39 // Tests that `.into()` isn't added when the error type
40 // matches the surrounding closure's return type, even
41 // when it doesn't match the surrounding function's.
closure_matches_test() -> Result<i32, i32>42 pub fn closure_matches_test() -> Result<i32, i32> {
43 let res: Result<i32, i8> = Some(1)
44 .into_iter()
45 .map(|i| {
46 let err: i8 = 1;
47 // To avoid warnings during rustfix
48 if true {
49 Err(err)?;
50 }
51 Ok(i)
52 })
53 .next()
54 .unwrap();
55
56 Ok(res?)
57 }
58
59 // Tests that `.into()` isn't added when the error type
60 // doesn't match the surrounding closure's return type.
closure_into_test() -> Result<i32, i32>61 pub fn closure_into_test() -> Result<i32, i32> {
62 let res: Result<i32, i16> = Some(1)
63 .into_iter()
64 .map(|i| {
65 let err: i8 = 1;
66 // To avoid warnings during rustfix
67 if true {
68 Err(err)?;
69 }
70 Ok(i)
71 })
72 .next()
73 .unwrap();
74
75 Ok(res?)
76 }
77
nested_error() -> Result<i32, i32>78 fn nested_error() -> Result<i32, i32> {
79 Ok(1)
80 }
81
82 #[inline_macros]
calling_macro() -> Result<i32, i32>83 fn calling_macro() -> Result<i32, i32> {
84 // macro
85 inline!(
86 match $(Ok::<_, i32>(5)) {
87 Ok(_) => 0,
88 Err(_) => Err(1)?,
89 }
90 );
91 // `Err` arg is another macro
92 inline!(
93 match $(Ok::<_, i32>(5)) {
94 Ok(_) => 0,
95 Err(_) => Err(inline!(1))?,
96 }
97 );
98 Ok(5)
99 }
100
main()101 fn main() {
102 basic_test().unwrap();
103 into_test().unwrap();
104 negative_test().unwrap();
105 closure_matches_test().unwrap();
106 closure_into_test().unwrap();
107 calling_macro().unwrap();
108
109 // We don't want to lint in external macros
110 external! {
111 pub fn try_err_fn() -> Result<i32, i32> {
112 let err: i32 = 1;
113 // To avoid warnings during rustfix
114 if true { Err(err)? } else { Ok(2) }
115 }
116 }
117 }
118
119 #[inline_macros]
macro_inside(fail: bool) -> Result<i32, String>120 pub fn macro_inside(fail: bool) -> Result<i32, String> {
121 if fail {
122 Err(inline!(inline!(String::from("aasdfasdfasdfa"))))?;
123 }
124 Ok(0)
125 }
126
poll_write(n: usize) -> Poll<io::Result<usize>>127 pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
128 if n == 0 {
129 Err(io::ErrorKind::WriteZero)?
130 } else if n == 1 {
131 Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
132 };
133
134 Poll::Ready(Ok(n))
135 }
136
poll_next(ready: bool) -> Poll<Option<io::Result<()>>>137 pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
138 if !ready {
139 Err(io::ErrorKind::NotFound)?
140 }
141
142 Poll::Ready(None)
143 }
144
145 // Tests that `return` is not duplicated
try_return(x: bool) -> Result<i32, i32>146 pub fn try_return(x: bool) -> Result<i32, i32> {
147 if x {
148 return Err(42)?;
149 }
150 Ok(0)
151 }
152