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()])
90 let optional = Some(5);
91 let _ = optional.map_or(5, |x| x + 2);
93 let _ = else_if_option(None);
94 unop_bad(&None, None);
95 let _ = longer_body(None);
96 test_map_or_else(None);
97 let _ = negative_tests(None);
98 let _ = impure_else(None);
100 let _ = Some(0).map_or(0, |x| loop {
107 const fn _f(x: Option<u32>) -> u32 {
108 // Don't lint, `map_or` isn't const
109 if let Some(x) = x { x } else { 10 }
113 let s = String::new();
114 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
115 let _ = if let Some(x) = Some(0) {
122 let s = String::new();
123 // Lint, both branches immutably borrow `s`.
124 let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
126 let s = String::new();
127 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
128 let _ = Some(0).map_or(1, |x| {
133 let s = Some(String::new());
134 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
135 let _ = if let Some(x) = &s {
142 let mut s = Some(String::new());
143 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
144 let _ = if let Some(x) = &mut s {
152 async fn _f1(x: u32) -> u32 {
157 // Don't lint. `await` can't be moved into a closure.
158 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
161 let _ = pattern_to_vec("hello world");