2020-07-14 12:59:59 +00:00
|
|
|
#![warn(clippy::option_if_let_else)]
|
2022-05-05 14:12:52 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::ref_option_ref,
|
|
|
|
clippy::equatable_if_let,
|
2023-07-31 21:53:53 +00:00
|
|
|
clippy::let_unit_value,
|
2024-03-08 21:04:17 +00:00
|
|
|
clippy::redundant_locals,
|
2024-06-08 13:34:59 +00:00
|
|
|
clippy::manual_unwrap_or_default,
|
|
|
|
clippy::manual_unwrap_or
|
2022-05-05 14:12:52 +00:00
|
|
|
)]
|
2020-07-14 12:59:59 +00:00
|
|
|
|
|
|
|
fn bad1(string: Option<&str>) -> (bool, &str) {
|
|
|
|
if let Some(x) = string {
|
|
|
|
(true, x)
|
|
|
|
} else {
|
|
|
|
(false, "hello")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
|
|
|
|
if string.is_none() {
|
|
|
|
None
|
|
|
|
} else if let Some(x) = string {
|
|
|
|
Some((true, x))
|
|
|
|
} else {
|
|
|
|
Some((false, ""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
|
|
|
|
let _ = if let Some(s) = *string { s.len() } else { 0 };
|
|
|
|
let _ = if let Some(s) = &num { s } else { &0 };
|
|
|
|
let _ = if let Some(s) = &mut num {
|
|
|
|
*s += 1;
|
|
|
|
s
|
|
|
|
} else {
|
2022-11-24 19:09:27 +00:00
|
|
|
&0
|
2020-07-14 12:59:59 +00:00
|
|
|
};
|
|
|
|
let _ = if let Some(ref s) = num { s } else { &0 };
|
|
|
|
let _ = if let Some(mut s) = num {
|
|
|
|
s += 1;
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let _ = if let Some(ref mut s) = num {
|
|
|
|
*s += 1;
|
|
|
|
s
|
|
|
|
} else {
|
2022-11-24 19:09:27 +00:00
|
|
|
&0
|
2020-07-14 12:59:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn longer_body(arg: Option<u32>) -> u32 {
|
|
|
|
if let Some(x) = arg {
|
|
|
|
let y = x * x;
|
|
|
|
y * y
|
|
|
|
} else {
|
|
|
|
13
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 12:49:22 +00:00
|
|
|
fn impure_else(arg: Option<i32>) {
|
|
|
|
let side_effect = || {
|
|
|
|
println!("return 1");
|
|
|
|
1
|
|
|
|
};
|
|
|
|
let _ = if let Some(x) = arg {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
// map_or_else must be suggested
|
|
|
|
side_effect()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:59 +00:00
|
|
|
fn test_map_or_else(arg: Option<u32>) {
|
|
|
|
let _ = if let Some(x) = arg {
|
|
|
|
x * x * x * x
|
|
|
|
} else {
|
|
|
|
let mut y = 1;
|
|
|
|
y = (y + 2 / y) / 2;
|
|
|
|
y = (y + 2 / y) / 2;
|
|
|
|
y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn negative_tests(arg: Option<u32>) -> u32 {
|
|
|
|
let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
|
|
|
|
for _ in 0..10 {
|
|
|
|
let _ = if let Some(x) = arg {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let _ = if let Some(x) = arg {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
5
|
|
|
|
};
|
|
|
|
7
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
// #7973
|
|
|
|
fn pattern_to_vec(pattern: &str) -> Vec<String> {
|
|
|
|
pattern
|
|
|
|
.trim_matches('/')
|
|
|
|
.split('/')
|
|
|
|
.flat_map(|s| {
|
|
|
|
if let Some(idx) = s.find('.') {
|
|
|
|
vec![s[..idx].to_string(), s[idx..].to_string()]
|
|
|
|
} else {
|
|
|
|
vec![s.to_string()]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
2023-05-20 13:39:26 +00:00
|
|
|
// #10335
|
2023-12-01 17:21:58 +00:00
|
|
|
fn test_result_impure_else(variable: Result<u32, &str>) -> bool {
|
2023-05-20 13:39:26 +00:00
|
|
|
if let Ok(binding) = variable {
|
|
|
|
println!("Ok {binding}");
|
2023-12-01 17:21:58 +00:00
|
|
|
true
|
2023-05-20 13:39:26 +00:00
|
|
|
} else {
|
|
|
|
println!("Err");
|
2023-12-01 17:21:58 +00:00
|
|
|
false
|
2023-05-20 13:39:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 12:40:22 +00:00
|
|
|
enum DummyEnum {
|
|
|
|
One(u8),
|
|
|
|
Two,
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:45:49 +00:00
|
|
|
// should not warn since there is a complex subpat
|
2021-12-17 12:40:22 +00:00
|
|
|
// see #7991
|
|
|
|
fn complex_subpat() -> DummyEnum {
|
|
|
|
let x = Some(DummyEnum::One(1));
|
|
|
|
let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
|
|
|
|
DummyEnum::Two
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:59 +00:00
|
|
|
fn main() {
|
|
|
|
let optional = Some(5);
|
|
|
|
let _ = if let Some(x) = optional { x + 2 } else { 5 };
|
|
|
|
let _ = bad1(None);
|
|
|
|
let _ = else_if_option(None);
|
|
|
|
unop_bad(&None, None);
|
|
|
|
let _ = longer_body(None);
|
|
|
|
test_map_or_else(None);
|
2023-05-20 13:39:26 +00:00
|
|
|
test_result_impure_else(Ok(42));
|
2020-07-14 12:59:59 +00:00
|
|
|
let _ = negative_tests(None);
|
2020-09-24 12:49:22 +00:00
|
|
|
let _ = impure_else(None);
|
2021-09-08 14:31:47 +00:00
|
|
|
|
|
|
|
let _ = if let Some(x) = Some(0) {
|
|
|
|
loop {
|
|
|
|
if x == 0 {
|
|
|
|
break x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
// #7576
|
|
|
|
const fn _f(x: Option<u32>) -> u32 {
|
|
|
|
// Don't lint, `map_or` isn't const
|
|
|
|
if let Some(x) = x { x } else { 10 }
|
|
|
|
}
|
|
|
|
|
|
|
|
// #5822
|
|
|
|
let s = String::new();
|
|
|
|
// Don't lint, `Some` branch consumes `s`, but else branch uses `s`
|
|
|
|
let _ = if let Some(x) = Some(0) {
|
|
|
|
let s = s;
|
|
|
|
s.len() + x
|
|
|
|
} else {
|
|
|
|
s.len()
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = String::new();
|
|
|
|
// Lint, both branches immutably borrow `s`.
|
|
|
|
let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
|
|
|
|
|
|
|
|
let s = String::new();
|
|
|
|
// Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
|
|
|
|
let _ = if let Some(x) = Some(0) {
|
|
|
|
let s = s;
|
|
|
|
s.len() + x
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = Some(String::new());
|
|
|
|
// Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
|
|
|
|
let _ = if let Some(x) = &s {
|
|
|
|
x.len()
|
|
|
|
} else {
|
|
|
|
let _s = s;
|
|
|
|
10
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut s = Some(String::new());
|
|
|
|
// Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
|
|
|
|
let _ = if let Some(x) = &mut s {
|
|
|
|
x.push_str("test");
|
|
|
|
x.len()
|
|
|
|
} else {
|
|
|
|
let _s = &s;
|
|
|
|
10
|
|
|
|
};
|
|
|
|
|
|
|
|
async fn _f1(x: u32) -> u32 {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn _f2() {
|
|
|
|
// Don't lint. `await` can't be moved into a closure.
|
|
|
|
let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
|
|
|
|
}
|
2021-12-06 11:33:31 +00:00
|
|
|
|
|
|
|
let _ = pattern_to_vec("hello world");
|
2021-12-17 12:40:22 +00:00
|
|
|
let _ = complex_subpat();
|
2022-08-31 13:24:45 +00:00
|
|
|
|
|
|
|
// issue #8492
|
|
|
|
let _ = match s {
|
|
|
|
Some(string) => string.len(),
|
|
|
|
None => 1,
|
|
|
|
};
|
|
|
|
let _ = match Some(10) {
|
|
|
|
Some(a) => a + 1,
|
|
|
|
None => 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res: Result<i32, i32> = Ok(5);
|
|
|
|
let _ = match res {
|
|
|
|
Ok(a) => a + 1,
|
|
|
|
_ => 1,
|
|
|
|
};
|
|
|
|
let _ = match res {
|
|
|
|
Err(_) => 1,
|
|
|
|
Ok(a) => a + 1,
|
|
|
|
};
|
|
|
|
let _ = if let Ok(a) = res { a + 1 } else { 5 };
|
2020-07-14 12:59:59 +00:00
|
|
|
}
|
2022-11-21 19:34:47 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn issue9742() -> Option<&'static str> {
|
|
|
|
// should not lint because of guards
|
|
|
|
match Some("foo ") {
|
|
|
|
Some(name) if name.starts_with("foo") => Some(name.trim()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2023-07-02 12:35:19 +00:00
|
|
|
|
|
|
|
mod issue10729 {
|
|
|
|
#![allow(clippy::unit_arg, dead_code)]
|
|
|
|
|
|
|
|
pub fn reproduce(initial: &Option<String>) {
|
|
|
|
// 👇 needs `.as_ref()` because initial is an `&Option<_>`
|
2023-12-01 17:21:58 +00:00
|
|
|
let _ = match initial {
|
2023-07-02 12:35:19 +00:00
|
|
|
Some(value) => do_something(value),
|
2023-12-01 17:21:58 +00:00
|
|
|
None => 42,
|
|
|
|
};
|
2023-07-02 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reproduce2(initial: &mut Option<String>) {
|
2023-12-01 17:21:58 +00:00
|
|
|
let _ = match initial {
|
2023-07-02 12:35:19 +00:00
|
|
|
Some(value) => do_something2(value),
|
2023-12-01 17:21:58 +00:00
|
|
|
None => 42,
|
|
|
|
};
|
2023-07-02 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:21:58 +00:00
|
|
|
fn do_something(_value: &str) -> u32 {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
fn do_something2(_value: &mut str) -> u32 {
|
|
|
|
todo!()
|
|
|
|
}
|
2023-07-02 12:35:19 +00:00
|
|
|
}
|
2023-11-02 16:35:56 +00:00
|
|
|
|
|
|
|
fn issue11429() {
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
macro_rules! new_map {
|
|
|
|
() => {{ HashMap::new() }};
|
|
|
|
}
|
|
|
|
|
|
|
|
let opt: Option<HashMap<u8, u8>> = None;
|
|
|
|
|
|
|
|
let mut _hashmap = if let Some(hm) = &opt {
|
|
|
|
hm.clone()
|
|
|
|
} else {
|
|
|
|
HashMap::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut _hm = if let Some(hm) = &opt { hm.clone() } else { new_map!() };
|
|
|
|
}
|
2023-12-01 17:21:58 +00:00
|
|
|
|
|
|
|
fn issue11893() {
|
|
|
|
use std::io::Write;
|
|
|
|
let mut output = std::io::stdout().lock();
|
|
|
|
if let Some(name) = Some("stuff") {
|
|
|
|
writeln!(output, "{name:?}").unwrap();
|
|
|
|
} else {
|
|
|
|
panic!("Haven't thought about this condition.");
|
|
|
|
}
|
|
|
|
}
|