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 string.map_or((false, "hello"), |x| (true, x))
9 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
12 } else if let Some(x) = string {
19 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
20 let _ = string.map_or(0, |s| s.len());
21 let _ = num.as_ref().map_or(&0, |s| s);
22 let _ = num.as_mut().map_or(&mut 0, |s| {
26 let _ = num.as_ref().map_or(&0, |s| s);
27 let _ = num.map_or(0, |mut s| {
31 let _ = num.as_mut().map_or(&mut 0, |s| {
37 fn longer_body(arg: Option<u32>) -> u32 {
44 fn impure_else(arg: Option<i32>) {
45 let side_effect = || {
49 let _ = arg.map_or_else(|| side_effect(), |x| x);
52 fn test_map_or_else(arg: Option<u32>) {
53 let _ = arg.map_or_else(|| {
58 }, |x| x * x * x * x);
61 fn negative_tests(arg: Option<u32>) -> u32 {
62 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
64 let _ = if let Some(x) = arg {
70 let _ = if let Some(x) = arg {
79 fn pattern_to_vec(pattern: &str) -> Vec<String> {
84 s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])
94 // should not warn since there is a compled complex subpat
96 fn complex_subpat() -> DummyEnum {
97 let x = Some(DummyEnum::One(1));
98 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
103 let optional = Some(5);
104 let _ = optional.map_or(5, |x| x + 2);
106 let _ = else_if_option(None);
107 unop_bad(&None, None);
108 let _ = longer_body(None);
109 test_map_or_else(None);
110 let _ = negative_tests(None);
111 let _ = impure_else(None);
113 let _ = Some(0).map_or(0, |x| loop {
120 const fn _f(x: Option<u32>) -> u32 {
121 // Don't lint, `map_or` isn't const
122 if let Some(x) = x { x } else { 10 }
126 let s = String::new();
127 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
128 let _ = if let Some(x) = Some(0) {
135 let s = String::new();
136 // Lint, both branches immutably borrow `s`.
137 let _ = Some(0).map_or(s.len(), |x| s.len() + x);
139 let s = String::new();
140 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
141 let _ = Some(0).map_or(1, |x| {
146 let s = Some(String::new());
147 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
148 let _ = if let Some(x) = &s {
155 let mut s = Some(String::new());
156 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
157 let _ = if let Some(x) = &mut s {
165 async fn _f1(x: u32) -> u32 {
170 // Don't lint. `await` can't be moved into a closure.
171 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
174 let _ = pattern_to_vec("hello world");
175 let _ = complex_subpat();