2 #![warn(clippy::option_if_let_else)]
4 clippy::redundant_closure,
5 clippy::ref_option_ref,
6 clippy::equatable_if_let,
10 fn bad1(string: Option<&str>) -> (bool, &str) {
11 if let Some(x) = string {
18 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
21 } else if let Some(x) = string {
28 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
29 let _ = if let Some(s) = *string { s.len() } else { 0 };
30 let _ = if let Some(s) = &num { s } else { &0 };
31 let _ = if let Some(s) = &mut num {
37 let _ = if let Some(ref s) = num { s } else { &0 };
38 let _ = if let Some(mut s) = num {
44 let _ = if let Some(ref mut s) = num {
52 fn longer_body(arg: Option<u32>) -> u32 {
53 if let Some(x) = arg {
61 fn impure_else(arg: Option<i32>) {
62 let side_effect = || {
66 let _ = if let Some(x) = arg {
69 // map_or_else must be suggested
74 fn test_map_or_else(arg: Option<u32>) {
75 let _ = if let Some(x) = arg {
85 fn negative_tests(arg: Option<u32>) -> u32 {
86 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
88 let _ = if let Some(x) = arg {
94 let _ = if let Some(x) = arg {
103 fn pattern_to_vec(pattern: &str) -> Vec<String> {
108 if let Some(idx) = s.find('.') {
109 vec![s[..idx].to_string(), s[idx..].to_string()]
122 // should not warn since there is a compled complex subpat
124 fn complex_subpat() -> DummyEnum {
125 let x = Some(DummyEnum::One(1));
126 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
131 let optional = Some(5);
132 let _ = if let Some(x) = optional { x + 2 } else { 5 };
134 let _ = else_if_option(None);
135 unop_bad(&None, None);
136 let _ = longer_body(None);
137 test_map_or_else(None);
138 let _ = negative_tests(None);
139 let _ = impure_else(None);
141 let _ = if let Some(x) = Some(0) {
152 const fn _f(x: Option<u32>) -> u32 {
153 // Don't lint, `map_or` isn't const
154 if let Some(x) = x { x } else { 10 }
158 let s = String::new();
159 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
160 let _ = if let Some(x) = Some(0) {
167 let s = String::new();
168 // Lint, both branches immutably borrow `s`.
169 let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
171 let s = String::new();
172 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
173 let _ = if let Some(x) = Some(0) {
180 let s = Some(String::new());
181 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
182 let _ = if let Some(x) = &s {
189 let mut s = Some(String::new());
190 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
191 let _ = if let Some(x) = &mut s {
199 async fn _f1(x: u32) -> u32 {
204 // Don't lint. `await` can't be moved into a closure.
205 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
208 let _ = pattern_to_vec("hello world");
209 let _ = complex_subpat();