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 let optional = Some(5);
80 let _ = optional.map_or(5, |x| x + 2);
82 let _ = else_if_option(None);
83 unop_bad(&None, None);
84 let _ = longer_body(None);
85 test_map_or_else(None);
86 let _ = negative_tests(None);
87 let _ = impure_else(None);
89 let _ = Some(0).map_or(0, |x| loop {
96 const fn _f(x: Option<u32>) -> u32 {
97 // Don't lint, `map_or` isn't const
98 if let Some(x) = x { x } else { 10 }
102 let s = String::new();
103 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
104 let _ = if let Some(x) = Some(0) {
111 let s = String::new();
112 // Lint, both branches immutably borrow `s`.
113 let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
115 let s = String::new();
116 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
117 let _ = Some(0).map_or(1, |x| {
122 let s = Some(String::new());
123 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
124 let _ = if let Some(x) = &s {
131 let mut s = Some(String::new());
132 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
133 let _ = if let Some(x) = &mut s {
141 async fn _f1(x: u32) -> u32 {
146 // Don't lint. `await` can't be moved into a closure.
147 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };