]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_question_mark.fixed
Auto merge of #8374 - Alexendoo:bless-revisions, r=camsteffen
[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 // not quite needless
98 fn deref_ref(s: Option<&String>) -> Option<&str> {
99     Some(s?)
100 }
101
102 fn main() {}
103
104 // #6921 if a macro wraps an expr in Some(  ) and the ? is in the macro use,
105 // the suggestion fails to apply; do not lint
106 macro_rules! some_in_macro {
107     ($expr:expr) => {
108         || -> _ { Some($expr) }()
109     };
110 }
111
112 pub fn test1() {
113     let x = Some(3);
114     let _x = some_in_macro!(x?);
115 }
116
117 // this one is ok because both the ? and the Some are both inside the macro def
118 macro_rules! some_and_qmark_in_macro {
119     ($expr:expr) => {
120         || -> Option<_> { Some($expr) }()
121     };
122 }
123
124 pub fn test2() {
125     let x = Some(3);
126     let _x = some_and_qmark_in_macro!(x?);
127 }
128
129 async fn async_option_bad(to: TO) -> Option<usize> {
130     let _ = Some(3);
131     to.magic
132 }
133
134 async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
135     Some(s?)
136 }
137
138 async fn async_result_bad(s: TR) -> Result<usize, bool> {
139     s.magic
140 }