]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_question_mark.fixed
Fix FN in `iter_cloned_collect` with a large array
[rust.git] / tests / ui / needless_question_mark.fixed
1 // run-rustfix
2
3 #![warn(clippy::needless_question_mark)]
4 #![allow(
5     clippy::needless_return,
6     clippy::unnecessary_unwrap,
7     clippy::upper_case_acronyms,
8     dead_code,
9     unused_must_use
10 )]
11 #![feature(custom_inner_attributes)]
12
13 struct TO {
14     magic: Option<usize>,
15 }
16
17 struct TR {
18     magic: Result<usize, bool>,
19 }
20
21 fn simple_option_bad1(to: TO) -> Option<usize> {
22     // return as a statement
23     return to.magic;
24 }
25
26 // formatting will add a semi-colon, which would make
27 // this identical to the test case above
28 #[rustfmt::skip]
29 fn simple_option_bad2(to: TO) -> Option<usize> {
30     // return as an expression
31     return to.magic
32 }
33
34 fn simple_option_bad3(to: TO) -> Option<usize> {
35     // block value "return"
36     to.magic
37 }
38
39 fn simple_option_bad4(to: Option<TO>) -> Option<usize> {
40     // single line closure
41     to.and_then(|t| t.magic)
42 }
43
44 // formatting this will remove the block brackets, making
45 // this test identical to the one above
46 #[rustfmt::skip]
47 fn simple_option_bad5(to: Option<TO>) -> Option<usize> {
48     // closure with body
49     to.and_then(|t| {
50         t.magic
51     })
52 }
53
54 fn simple_result_bad1(tr: TR) -> Result<usize, bool> {
55     return tr.magic;
56 }
57
58 // formatting will add a semi-colon, which would make
59 // this identical to the test case above
60 #[rustfmt::skip]
61 fn simple_result_bad2(tr: TR) -> Result<usize, bool> {
62     return tr.magic
63 }
64
65 fn simple_result_bad3(tr: TR) -> Result<usize, bool> {
66     tr.magic
67 }
68
69 fn simple_result_bad4(tr: Result<TR, bool>) -> Result<usize, bool> {
70     tr.and_then(|t| t.magic)
71 }
72
73 // formatting this will remove the block brackets, making
74 // this test identical to the one above
75 #[rustfmt::skip]
76 fn simple_result_bad5(tr: Result<TR, bool>) -> Result<usize, bool> {
77     tr.and_then(|t| {
78         t.magic
79     })
80 }
81
82 fn also_bad(tr: Result<TR, bool>) -> Result<usize, bool> {
83     if tr.is_ok() {
84         let t = tr.unwrap();
85         return t.magic;
86     }
87     Err(false)
88 }
89
90 fn false_positive_test<U, T>(x: Result<(), U>) -> Result<(), T>
91 where
92     T: From<U>,
93 {
94     Ok(x?)
95 }
96
97 fn main() {}
98
99 mod question_mark_none {
100     #![clippy::msrv = "1.12.0"]
101     fn needless_question_mark_option() -> Option<usize> {
102         struct TO {
103             magic: Option<usize>,
104         }
105         let to = TO { magic: None };
106         Some(to.magic?) // should not be triggered
107     }
108
109     fn needless_question_mark_result() -> Result<usize, bool> {
110         struct TO {
111             magic: Result<usize, bool>,
112         }
113         let to = TO { magic: Ok(1_usize) };
114         Ok(to.magic?) // should not be triggered
115     }
116
117     fn main() {
118         needless_question_mark_option();
119         needless_question_mark_result();
120     }
121 }
122
123 mod question_mark_result {
124     #![clippy::msrv = "1.21.0"]
125     fn needless_question_mark_option() -> Option<usize> {
126         struct TO {
127             magic: Option<usize>,
128         }
129         let to = TO { magic: None };
130         Some(to.magic?) // should not be triggered
131     }
132
133     fn needless_question_mark_result() -> Result<usize, bool> {
134         struct TO {
135             magic: Result<usize, bool>,
136         }
137         let to = TO { magic: Ok(1_usize) };
138         to.magic // should be triggered
139     }
140
141     fn main() {
142         needless_question_mark_option();
143         needless_question_mark_result();
144     }
145 }
146
147 mod question_mark_both {
148     #![clippy::msrv = "1.22.0"]
149     fn needless_question_mark_option() -> Option<usize> {
150         struct TO {
151             magic: Option<usize>,
152         }
153         let to = TO { magic: None };
154         to.magic // should be triggered
155     }
156
157     fn needless_question_mark_result() -> Result<usize, bool> {
158         struct TO {
159             magic: Result<usize, bool>,
160         }
161         let to = TO { magic: Ok(1_usize) };
162         to.magic // should be triggered
163     }
164
165     fn main() {
166         needless_question_mark_option();
167         needless_question_mark_result();
168     }
169 }
170
171 // #6921 if a macro wraps an expr in Some(  ) and the ? is in the macro use,
172 // the suggestion fails to apply; do not lint
173 macro_rules! some_in_macro {
174     ($expr:expr) => {
175         || -> _ { Some($expr) }()
176     };
177 }
178
179 pub fn test1() {
180     let x = Some(3);
181     let _x = some_in_macro!(x?);
182 }
183
184 // this one is ok because both the ? and the Some are both inside the macro def
185 macro_rules! some_and_qmark_in_macro {
186     ($expr:expr) => {
187         || -> Option<_> { Some($expr) }()
188     };
189 }
190
191 pub fn test2() {
192     let x = Some(3);
193     let _x = some_and_qmark_in_macro!(x?);
194 }