2 #![warn(clippy::option_if_let_else)]
4 unused_tuple_struct_fields,
5 clippy::redundant_closure,
6 clippy::ref_option_ref,
7 clippy::equatable_if_let,
11 fn bad1(string: Option<&str>) -> (bool, &str) {
12 if let Some(x) = string {
19 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
22 } else if let Some(x) = string {
29 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
30 let _ = if let Some(s) = *string { s.len() } else { 0 };
31 let _ = if let Some(s) = &num { s } else { &0 };
32 let _ = if let Some(s) = &mut num {
38 let _ = if let Some(ref s) = num { s } else { &0 };
39 let _ = if let Some(mut s) = num {
45 let _ = if let Some(ref mut s) = num {
53 fn longer_body(arg: Option<u32>) -> u32 {
54 if let Some(x) = arg {
62 fn impure_else(arg: Option<i32>) {
63 let side_effect = || {
67 let _ = if let Some(x) = arg {
70 // map_or_else must be suggested
75 fn test_map_or_else(arg: Option<u32>) {
76 let _ = if let Some(x) = arg {
86 fn negative_tests(arg: Option<u32>) -> u32 {
87 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
89 let _ = if let Some(x) = arg {
95 let _ = if let Some(x) = arg {
104 fn pattern_to_vec(pattern: &str) -> Vec<String> {
109 if let Some(idx) = s.find('.') {
110 vec![s[..idx].to_string(), s[idx..].to_string()]
123 // should not warn since there is a compled complex subpat
125 fn complex_subpat() -> DummyEnum {
126 let x = Some(DummyEnum::One(1));
127 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
132 let optional = Some(5);
133 let _ = if let Some(x) = optional { x + 2 } else { 5 };
135 let _ = else_if_option(None);
136 unop_bad(&None, None);
137 let _ = longer_body(None);
138 test_map_or_else(None);
139 let _ = negative_tests(None);
140 let _ = impure_else(None);
142 let _ = if let Some(x) = Some(0) {
153 const fn _f(x: Option<u32>) -> u32 {
154 // Don't lint, `map_or` isn't const
155 if let Some(x) = x { x } else { 10 }
159 let s = String::new();
160 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
161 let _ = if let Some(x) = Some(0) {
168 let s = String::new();
169 // Lint, both branches immutably borrow `s`.
170 let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
172 let s = String::new();
173 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
174 let _ = if let Some(x) = Some(0) {
181 let s = Some(String::new());
182 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
183 let _ = if let Some(x) = &s {
190 let mut s = Some(String::new());
191 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
192 let _ = if let Some(x) = &mut s {
200 async fn _f1(x: u32) -> u32 {
205 // Don't lint. `await` can't be moved into a closure.
206 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
209 let _ = pattern_to_vec("hello world");
210 let _ = complex_subpat();
214 Some(string) => string.len(),
217 let _ = match Some(10) {
222 let res: Result<i32, i32> = Ok(5);
231 let _ = if let Ok(a) = res { a + 1 } else { 5 };