]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_question_mark.rs
Auto merge of #7136 - mgacek8:issue6965_manual_unwrap_or_invalid_sugg_macro_expansion...
[rust.git] / tests / ui / needless_question_mark.rs
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 Some(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 Some(to.magic?)
32 }
33
34 fn simple_option_bad3(to: TO) -> Option<usize> {
35     // block value "return"
36     Some(to.magic?)
37 }
38
39 fn simple_option_bad4(to: Option<TO>) -> Option<usize> {
40     // single line closure
41     to.and_then(|t| Some(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         Some(t.magic?)
51     })
52 }
53
54 fn simple_result_bad1(tr: TR) -> Result<usize, bool> {
55     return Ok(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 Ok(tr.magic?)
63 }
64
65 fn simple_result_bad3(tr: TR) -> Result<usize, bool> {
66     Ok(tr.magic?)
67 }
68
69 fn simple_result_bad4(tr: Result<TR, bool>) -> Result<usize, bool> {
70     tr.and_then(|t| Ok(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         Ok(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 Ok(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 // #6921 if a macro wraps an expr in Some(  ) and the ? is in the macro use,
100 // the suggestion fails to apply; do not lint
101 macro_rules! some_in_macro {
102     ($expr:expr) => {
103         || -> _ { Some($expr) }()
104     };
105 }
106
107 pub fn test1() {
108     let x = Some(3);
109     let _x = some_in_macro!(x?);
110 }
111
112 // this one is ok because both the ? and the Some are both inside the macro def
113 macro_rules! some_and_qmark_in_macro {
114     ($expr:expr) => {
115         || -> Option<_> { Some(Some($expr)?) }()
116     };
117 }
118
119 pub fn test2() {
120     let x = Some(3);
121     let _x = some_and_qmark_in_macro!(x?);
122 }