3 #![warn(clippy::option_if_let_else)]
4 #![allow(clippy::redundant_closure)]
5 #![allow(clippy::ref_option_ref, clippy::equatable_if_let)]
7 fn bad1(string: Option<&str>) -> (bool, &str) {
8 string.map_or((false, "hello"), |x| (true, x))
11 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
14 } else if let Some(x) = string {
21 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
22 let _ = string.map_or(0, |s| s.len());
23 let _ = num.as_ref().map_or(&0, |s| s);
24 let _ = num.as_mut().map_or(&mut 0, |s| {
28 let _ = num.as_ref().map_or(&0, |s| s);
29 let _ = num.map_or(0, |mut s| {
33 let _ = num.as_mut().map_or(&mut 0, |s| {
39 fn longer_body(arg: Option<u32>) -> u32 {
46 fn impure_else(arg: Option<i32>) {
47 let side_effect = || {
51 let _ = arg.map_or_else(|| side_effect(), |x| x);
54 fn test_map_or_else(arg: Option<u32>) {
55 let _ = arg.map_or_else(|| {
60 }, |x| x * x * x * x);
63 fn negative_tests(arg: Option<u32>) -> u32 {
64 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
66 let _ = if let Some(x) = arg {
72 let _ = if let Some(x) = arg {
81 let optional = Some(5);
82 let _ = optional.map_or(5, |x| x + 2);
84 let _ = else_if_option(None);
85 unop_bad(&None, None);
86 let _ = longer_body(None);
87 test_map_or_else(None);
88 let _ = negative_tests(None);
89 let _ = impure_else(None);
91 let _ = Some(0).map_or(0, |x| loop {
98 const fn _f(x: Option<u32>) -> u32 {
99 // Don't lint, `map_or` isn't const
100 if let Some(x) = x { x } else { 10 }
104 let s = String::new();
105 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
106 let _ = if let Some(x) = Some(0) {
113 let s = String::new();
114 // Lint, both branches immutably borrow `s`.
115 let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
117 let s = String::new();
118 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
119 let _ = Some(0).map_or(1, |x| {
124 let s = Some(String::new());
125 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
126 let _ = if let Some(x) = &s {
133 let mut s = Some(String::new());
134 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
135 let _ = if let Some(x) = &mut s {
143 async fn _f1(x: u32) -> u32 {
148 // Don't lint. `await` can't be moved into a closure.
149 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };