1 #![warn(clippy::option_if_let_else)]
3 unused_tuple_struct_fields,
4 clippy::redundant_closure,
5 clippy::ref_option_ref,
6 clippy::equatable_if_let,
7 clippy::let_unit_value,
8 clippy::redundant_locals
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(&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(&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()])
96 fn test_result_impure_else(variable: Result<u32, &str>) {
97 variable.map_or_else(|_| {
100 println!("Ok {binding}");
109 // should not warn since there is a complex subpat
111 fn complex_subpat() -> DummyEnum {
112 let x = Some(DummyEnum::One(1));
113 let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
118 let optional = Some(5);
119 let _ = optional.map_or(5, |x| x + 2);
121 let _ = else_if_option(None);
122 unop_bad(&None, None);
123 let _ = longer_body(None);
124 test_map_or_else(None);
125 test_result_impure_else(Ok(42));
126 let _ = negative_tests(None);
127 let _ = impure_else(None);
129 let _ = Some(0).map_or(0, |x| loop {
136 const fn _f(x: Option<u32>) -> u32 {
137 // Don't lint, `map_or` isn't const
138 if let Some(x) = x { x } else { 10 }
142 let s = String::new();
143 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
144 let _ = if let Some(x) = Some(0) {
151 let s = String::new();
152 // Lint, both branches immutably borrow `s`.
153 let _ = Some(0).map_or(s.len(), |x| s.len() + x);
155 let s = String::new();
156 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
157 let _ = Some(0).map_or(1, |x| {
162 let s = Some(String::new());
163 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
164 let _ = if let Some(x) = &s {
171 let mut s = Some(String::new());
172 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
173 let _ = if let Some(x) = &mut s {
181 async fn _f1(x: u32) -> u32 {
186 // Don't lint. `await` can't be moved into a closure.
187 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
190 let _ = pattern_to_vec("hello world");
191 let _ = complex_subpat();
194 let _ = s.map_or(1, |string| string.len());
195 let _ = Some(10).map_or(5, |a| a + 1);
197 let res: Result<i32, i32> = Ok(5);
198 let _ = res.map_or(1, |a| a + 1);
199 let _ = res.map_or(1, |a| a + 1);
200 let _ = res.map_or(5, |a| a + 1);
204 fn issue9742() -> Option<&'static str> {
205 // should not lint because of guards
207 Some(name) if name.starts_with("foo") => Some(name.trim()),
213 #![allow(clippy::unit_arg, dead_code)]
215 pub fn reproduce(initial: &Option<String>) {
216 // 👇 needs `.as_ref()` because initial is an `&Option<_>`
217 initial.as_ref().map_or({}, |value| do_something(value))
220 pub fn reproduce2(initial: &mut Option<String>) {
221 initial.as_mut().map_or({}, |value| do_something2(value))
224 fn do_something(_value: &str) {}
225 fn do_something2(_value: &mut str) {}