2 #![warn(clippy::option_if_let_else)]
4 clippy::redundant_closure,
5 clippy::ref_option_ref,
6 clippy::equatable_if_let,
10 fn bad1(string: Option<&str>) -> (bool, &str) {
11 string.map_or((false, "hello"), |x| (true, x))
14 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
17 } else if let Some(x) = string {
24 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
25 let _ = string.map_or(0, |s| s.len());
26 let _ = num.as_ref().map_or(&0, |s| s);
27 let _ = num.as_mut().map_or(&mut 0, |s| {
31 let _ = num.as_ref().map_or(&0, |s| s);
32 let _ = num.map_or(0, |mut s| {
36 let _ = num.as_mut().map_or(&mut 0, |s| {
42 fn longer_body(arg: Option<u32>) -> u32 {
49 fn impure_else(arg: Option<i32>) {
50 let side_effect = || {
54 let _ = arg.map_or_else(|| side_effect(), |x| x);
57 fn test_map_or_else(arg: Option<u32>) {
58 let _ = arg.map_or_else(|| {
63 }, |x| x * x * x * x);
66 fn negative_tests(arg: Option<u32>) -> u32 {
67 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
69 let _ = if let Some(x) = arg {
75 let _ = if let Some(x) = arg {
84 fn pattern_to_vec(pattern: &str) -> Vec<String> {
89 s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])
99 // should not warn since there is a compled complex subpat
101 fn complex_subpat() -> DummyEnum {
102 let x = Some(DummyEnum::One(1));
103 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
108 let optional = Some(5);
109 let _ = optional.map_or(5, |x| x + 2);
111 let _ = else_if_option(None);
112 unop_bad(&None, None);
113 let _ = longer_body(None);
114 test_map_or_else(None);
115 let _ = negative_tests(None);
116 let _ = impure_else(None);
118 let _ = Some(0).map_or(0, |x| loop {
125 const fn _f(x: Option<u32>) -> u32 {
126 // Don't lint, `map_or` isn't const
127 if let Some(x) = x { x } else { 10 }
131 let s = String::new();
132 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
133 let _ = if let Some(x) = Some(0) {
140 let s = String::new();
141 // Lint, both branches immutably borrow `s`.
142 let _ = Some(0).map_or(s.len(), |x| s.len() + x);
144 let s = String::new();
145 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
146 let _ = Some(0).map_or(1, |x| {
151 let s = Some(String::new());
152 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
153 let _ = if let Some(x) = &s {
160 let mut s = Some(String::new());
161 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
162 let _ = if let Some(x) = &mut s {
170 async fn _f1(x: u32) -> u32 {
175 // Don't lint. `await` can't be moved into a closure.
176 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
179 let _ = pattern_to_vec("hello world");
180 let _ = complex_subpat();