3 #![warn(clippy::option_if_let_else)]
4 #![allow(clippy::redundant_closure, clippy::ref_option_ref, clippy::equatable_if_let)]
6 fn bad1(string: Option<&str>) -> (bool, &str) {
7 string.map_or((false, "hello"), |x| (true, x))
10 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
13 } else if let Some(x) = string {
20 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
21 let _ = string.map_or(0, |s| s.len());
22 let _ = num.as_ref().map_or(&0, |s| s);
23 let _ = num.as_mut().map_or(&mut 0, |s| {
27 let _ = num.as_ref().map_or(&0, |s| s);
28 let _ = num.map_or(0, |mut s| {
32 let _ = num.as_mut().map_or(&mut 0, |s| {
38 fn longer_body(arg: Option<u32>) -> u32 {
45 fn impure_else(arg: Option<i32>) {
46 let side_effect = || {
50 let _ = arg.map_or_else(|| side_effect(), |x| x);
53 fn test_map_or_else(arg: Option<u32>) {
54 let _ = arg.map_or_else(|| {
59 }, |x| x * x * x * x);
62 fn negative_tests(arg: Option<u32>) -> u32 {
63 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
65 let _ = if let Some(x) = arg {
71 let _ = if let Some(x) = arg {
80 let optional = Some(5);
81 let _ = optional.map_or(5, |x| x + 2);
83 let _ = else_if_option(None);
84 unop_bad(&None, None);
85 let _ = longer_body(None);
86 test_map_or_else(None);
87 let _ = negative_tests(None);
88 let _ = impure_else(None);
90 let _ = Some(0).map_or(0, |x| loop {
97 const fn _f(x: Option<u32>) -> u32 {
98 // Don't lint, `map_or` isn't const
99 if let Some(x) = x { x } else { 10 }
103 let s = String::new();
104 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
105 let _ = if let Some(x) = Some(0) {
112 let s = String::new();
113 // Lint, both branches immutably borrow `s`.
114 let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
116 let s = String::new();
117 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
118 let _ = Some(0).map_or(1, |x| {
123 let s = Some(String::new());
124 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
125 let _ = if let Some(x) = &s {
132 let mut s = Some(String::new());
133 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
134 let _ = if let Some(x) = &mut s {
142 async fn _f1(x: u32) -> u32 {
147 // Don't lint. `await` can't be moved into a closure.
148 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };