2 #![warn(clippy::option_if_let_else)]
4 unused_tuple_struct_fields,
5 clippy::redundant_closure,
6 clippy::ref_option_ref,
7 clippy::equatable_if_let,
11 fn bad1(string: Option<&str>) -> (bool, &str) {
12 string.map_or((false, "hello"), |x| (true, x))
15 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
18 } else if let Some(x) = string {
25 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
26 let _ = string.map_or(0, |s| s.len());
27 let _ = num.as_ref().map_or(&0, |s| s);
28 let _ = num.as_mut().map_or(&mut 0, |s| {
32 let _ = num.as_ref().map_or(&0, |s| s);
33 let _ = num.map_or(0, |mut s| {
37 let _ = num.as_mut().map_or(&mut 0, |s| {
43 fn longer_body(arg: Option<u32>) -> u32 {
50 fn impure_else(arg: Option<i32>) {
51 let side_effect = || {
55 let _ = arg.map_or_else(|| side_effect(), |x| x);
58 fn test_map_or_else(arg: Option<u32>) {
59 let _ = arg.map_or_else(|| {
64 }, |x| x * x * x * x);
67 fn negative_tests(arg: Option<u32>) -> u32 {
68 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
70 let _ = if let Some(x) = arg {
76 let _ = if let Some(x) = arg {
85 fn pattern_to_vec(pattern: &str) -> Vec<String> {
90 s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])
100 // should not warn since there is a compled complex subpat
102 fn complex_subpat() -> DummyEnum {
103 let x = Some(DummyEnum::One(1));
104 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
109 let optional = Some(5);
110 let _ = optional.map_or(5, |x| x + 2);
112 let _ = else_if_option(None);
113 unop_bad(&None, None);
114 let _ = longer_body(None);
115 test_map_or_else(None);
116 let _ = negative_tests(None);
117 let _ = impure_else(None);
119 let _ = Some(0).map_or(0, |x| loop {
126 const fn _f(x: Option<u32>) -> u32 {
127 // Don't lint, `map_or` isn't const
128 if let Some(x) = x { x } else { 10 }
132 let s = String::new();
133 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
134 let _ = if let Some(x) = Some(0) {
141 let s = String::new();
142 // Lint, both branches immutably borrow `s`.
143 let _ = Some(0).map_or(s.len(), |x| s.len() + x);
145 let s = String::new();
146 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
147 let _ = Some(0).map_or(1, |x| {
152 let s = Some(String::new());
153 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
154 let _ = if let Some(x) = &s {
161 let mut s = Some(String::new());
162 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
163 let _ = if let Some(x) = &mut s {
171 async fn _f1(x: u32) -> u32 {
176 // Don't lint. `await` can't be moved into a closure.
177 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
180 let _ = pattern_to_vec("hello world");
181 let _ = complex_subpat();
184 let _ = s.map_or(1, |string| string.len());
185 let _ = Some(10).map_or(5, |a| a + 1);
187 let res: Result<i32, i32> = Ok(5);
188 let _ = res.map_or(1, |a| a + 1);
189 let _ = res.map_or(1, |a| a + 1);
190 let _ = res.map_or(5, |a| a + 1);
194 fn issue9742() -> Option<&'static str> {
195 // should not lint because of guards
197 Some(name) if name.starts_with("foo") => Some(name.trim()),