2 #![warn(clippy::option_if_let_else)]
3 #![allow(clippy::redundant_closure, clippy::ref_option_ref, clippy::equatable_if_let)]
5 fn bad1(string: Option<&str>) -> (bool, &str) {
6 if let Some(x) = string {
13 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
16 } else if let Some(x) = string {
23 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
24 let _ = if let Some(s) = *string { s.len() } else { 0 };
25 let _ = if let Some(s) = &num { s } else { &0 };
26 let _ = if let Some(s) = &mut num {
32 let _ = if let Some(ref s) = num { s } else { &0 };
33 let _ = if let Some(mut s) = num {
39 let _ = if let Some(ref mut s) = num {
47 fn longer_body(arg: Option<u32>) -> u32 {
48 if let Some(x) = arg {
56 fn impure_else(arg: Option<i32>) {
57 let side_effect = || {
61 let _ = if let Some(x) = arg {
64 // map_or_else must be suggested
69 fn test_map_or_else(arg: Option<u32>) {
70 let _ = if let Some(x) = arg {
80 fn negative_tests(arg: Option<u32>) -> u32 {
81 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
83 let _ = if let Some(x) = arg {
89 let _ = if let Some(x) = arg {
98 fn pattern_to_vec(pattern: &str) -> Vec<String> {
103 if let Some(idx) = s.find('.') {
104 vec![s[..idx].to_string(), s[idx..].to_string()]
117 // should not warn since there is a compled complex subpat
119 fn complex_subpat() -> DummyEnum {
120 let x = Some(DummyEnum::One(1));
121 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
126 let optional = Some(5);
127 let _ = if let Some(x) = optional { x + 2 } else { 5 };
129 let _ = else_if_option(None);
130 unop_bad(&None, None);
131 let _ = longer_body(None);
132 test_map_or_else(None);
133 let _ = negative_tests(None);
134 let _ = impure_else(None);
136 let _ = if let Some(x) = Some(0) {
147 const fn _f(x: Option<u32>) -> u32 {
148 // Don't lint, `map_or` isn't const
149 if let Some(x) = x { x } else { 10 }
153 let s = String::new();
154 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
155 let _ = if let Some(x) = Some(0) {
162 let s = String::new();
163 // Lint, both branches immutably borrow `s`.
164 let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
166 let s = String::new();
167 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
168 let _ = if let Some(x) = Some(0) {
175 let s = Some(String::new());
176 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
177 let _ = if let Some(x) = &s {
184 let mut s = Some(String::new());
185 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
186 let _ = if let Some(x) = &mut s {
194 async fn _f1(x: u32) -> u32 {
199 // Don't lint. `await` can't be moved into a closure.
200 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
203 let _ = pattern_to_vec("hello world");
204 let _ = complex_subpat();