]>
Commit | Line | Data |
---|---|---|
781aab86 | 1 | //@no-rustfix: overlapping suggestions |
064997fb | 2 | #![feature(lint_reasons)] |
f20569fa | 3 | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)] |
fe692bf9 FG |
4 | #![allow( |
5 | clippy::if_same_then_else, | |
6 | clippy::branches_sharing_code, | |
7 | clippy::unnecessary_literal_unwrap | |
8 | )] | |
f20569fa XL |
9 | |
10 | macro_rules! m { | |
11 | ($a:expr) => { | |
12 | if $a.is_some() { | |
781aab86 FG |
13 | // unnecessary |
14 | $a.unwrap(); | |
f20569fa XL |
15 | } |
16 | }; | |
17 | } | |
18 | ||
19 | macro_rules! checks_in_param { | |
20 | ($a:expr, $b:expr) => { | |
21 | if $a { | |
22 | $b; | |
23 | } | |
24 | }; | |
25 | } | |
26 | ||
27 | macro_rules! checks_unwrap { | |
28 | ($a:expr, $b:expr) => { | |
29 | if $a.is_some() { | |
30 | $b; | |
31 | } | |
32 | }; | |
33 | } | |
34 | ||
35 | macro_rules! checks_some { | |
36 | ($a:expr, $b:expr) => { | |
37 | if $a { | |
38 | $b.unwrap(); | |
39 | } | |
40 | }; | |
41 | } | |
42 | ||
43 | fn main() { | |
44 | let x = Some(()); | |
45 | if x.is_some() { | |
781aab86 FG |
46 | // unnecessary |
47 | x.unwrap(); | |
48 | //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_some` | |
49 | // unnecessary | |
50 | x.expect("an error message"); | |
51 | //~^ ERROR: called `expect` on `x` after checking its variant with `is_some` | |
f20569fa | 52 | } else { |
781aab86 FG |
53 | // will panic |
54 | x.unwrap(); | |
55 | //~^ ERROR: this call to `unwrap()` will always panic | |
56 | // will panic | |
57 | x.expect("an error message"); | |
58 | //~^ ERROR: this call to `expect()` will always panic | |
f20569fa XL |
59 | } |
60 | if x.is_none() { | |
781aab86 FG |
61 | // will panic |
62 | x.unwrap(); | |
63 | //~^ ERROR: this call to `unwrap()` will always panic | |
f20569fa | 64 | } else { |
781aab86 FG |
65 | // unnecessary |
66 | x.unwrap(); | |
67 | //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_none` | |
f20569fa XL |
68 | } |
69 | m!(x); | |
781aab86 FG |
70 | // ok |
71 | checks_in_param!(x.is_some(), x.unwrap()); | |
72 | // ok | |
73 | checks_unwrap!(x, x.unwrap()); | |
74 | // ok | |
75 | checks_some!(x.is_some(), x); | |
f20569fa XL |
76 | let mut x: Result<(), ()> = Ok(()); |
77 | if x.is_ok() { | |
781aab86 FG |
78 | // unnecessary |
79 | x.unwrap(); | |
80 | //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_ok` | |
81 | // unnecessary | |
82 | x.expect("an error message"); | |
83 | //~^ ERROR: called `expect` on `x` after checking its variant with `is_ok` | |
84 | // will panic | |
85 | x.unwrap_err(); | |
86 | //~^ ERROR: this call to `unwrap_err()` will always panic | |
f20569fa | 87 | } else { |
781aab86 FG |
88 | // will panic |
89 | x.unwrap(); | |
90 | //~^ ERROR: this call to `unwrap()` will always panic | |
91 | // will panic | |
92 | x.expect("an error message"); | |
93 | //~^ ERROR: this call to `expect()` will always panic | |
94 | // unnecessary | |
95 | x.unwrap_err(); | |
96 | //~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_ok` | |
f20569fa XL |
97 | } |
98 | if x.is_err() { | |
781aab86 FG |
99 | // will panic |
100 | x.unwrap(); | |
101 | //~^ ERROR: this call to `unwrap()` will always panic | |
102 | // unnecessary | |
103 | x.unwrap_err(); | |
104 | //~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_err` | |
f20569fa | 105 | } else { |
781aab86 FG |
106 | // unnecessary |
107 | x.unwrap(); | |
108 | //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_err` | |
109 | // will panic | |
110 | x.unwrap_err(); | |
111 | //~^ ERROR: this call to `unwrap_err()` will always panic | |
f20569fa XL |
112 | } |
113 | if x.is_ok() { | |
114 | x = Err(()); | |
115 | // not unnecessary because of mutation of x | |
116 | // it will always panic but the lint is not smart enough to see this (it only | |
117 | // checks if conditions). | |
118 | x.unwrap(); | |
119 | } else { | |
120 | x = Ok(()); | |
121 | // not unnecessary because of mutation of x | |
122 | // it will always panic but the lint is not smart enough to see this (it | |
123 | // only checks if conditions). | |
124 | x.unwrap_err(); | |
125 | } | |
126 | ||
781aab86 FG |
127 | // ok, it's a common test pattern |
128 | assert!(x.is_ok(), "{:?}", x.unwrap_err()); | |
129 | } | |
130 | ||
131 | fn issue11371() { | |
132 | let option = Some(()); | |
133 | ||
134 | if option.is_some() { | |
135 | option.as_ref().unwrap(); | |
136 | //~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some` | |
137 | } else { | |
138 | option.as_ref().unwrap(); | |
139 | //~^ ERROR: this call to `unwrap()` will always panic | |
140 | } | |
141 | ||
142 | let result = Ok::<(), ()>(()); | |
143 | ||
144 | if result.is_ok() { | |
145 | result.as_ref().unwrap(); | |
146 | //~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok` | |
147 | } else { | |
148 | result.as_ref().unwrap(); | |
149 | //~^ ERROR: this call to `unwrap()` will always panic | |
150 | } | |
151 | ||
152 | let mut option = Some(()); | |
153 | if option.is_some() { | |
154 | option.as_mut().unwrap(); | |
155 | //~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some` | |
156 | } else { | |
157 | option.as_mut().unwrap(); | |
158 | //~^ ERROR: this call to `unwrap()` will always panic | |
159 | } | |
160 | ||
161 | let mut result = Ok::<(), ()>(()); | |
162 | if result.is_ok() { | |
163 | result.as_mut().unwrap(); | |
164 | //~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok` | |
165 | } else { | |
166 | result.as_mut().unwrap(); | |
167 | //~^ ERROR: this call to `unwrap()` will always panic | |
168 | } | |
169 | ||
170 | // This should not lint. Statics are, at the time of writing, not linted on anyway, | |
171 | // but if at some point they are supported by this lint, it should correctly see that | |
172 | // `X` is being mutated and not suggest `if let Some(..) = X {}` | |
173 | static mut X: Option<i32> = Some(123); | |
174 | unsafe { | |
175 | if X.is_some() { | |
176 | X = None; | |
177 | X.unwrap(); | |
178 | } | |
179 | } | |
f20569fa | 180 | } |
064997fb FG |
181 | |
182 | fn check_expect() { | |
183 | let x = Some(()); | |
184 | if x.is_some() { | |
185 | #[expect(clippy::unnecessary_unwrap)] | |
781aab86 FG |
186 | // unnecessary |
187 | x.unwrap(); | |
064997fb | 188 | #[expect(clippy::unnecessary_unwrap)] |
781aab86 FG |
189 | // unnecessary |
190 | x.expect("an error message"); | |
064997fb FG |
191 | } else { |
192 | #[expect(clippy::panicking_unwrap)] | |
781aab86 FG |
193 | // will panic |
194 | x.unwrap(); | |
064997fb | 195 | #[expect(clippy::panicking_unwrap)] |
781aab86 FG |
196 | // will panic |
197 | x.expect("an error message"); | |
064997fb FG |
198 | } |
199 | } |