2024-03-09 18:12:15 -06:00
|
|
|
#![warn(clippy::manual_unwrap_or_default)]
|
2024-03-08 15:04:31 -06:00
|
|
|
#![allow(clippy::unnecessary_literal_unwrap)]
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x: Option<Vec<String>> = None;
|
|
|
|
match x {
|
|
|
|
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
|
|
|
|
Some(v) => v,
|
|
|
|
None => Vec::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let x: Option<Vec<String>> = None;
|
|
|
|
match x {
|
|
|
|
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
|
|
|
|
Some(v) => v,
|
|
|
|
_ => Vec::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let x: Option<String> = None;
|
|
|
|
match x {
|
|
|
|
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
|
|
|
|
Some(v) => v,
|
|
|
|
None => String::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let x: Option<Vec<String>> = None;
|
|
|
|
match x {
|
|
|
|
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
|
|
|
|
None => Vec::default(),
|
|
|
|
Some(v) => v,
|
|
|
|
};
|
|
|
|
|
|
|
|
let x: Option<Vec<String>> = None;
|
|
|
|
if let Some(v) = x {
|
|
|
|
//~^ ERROR: if let can be simplified with `.unwrap_or_default()`
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
Vec::default()
|
|
|
|
};
|
2024-04-01 09:48:03 -05:00
|
|
|
|
|
|
|
// Issue #12564
|
|
|
|
// No error as &Vec<_> doesn't implement std::default::Default
|
|
|
|
let mut map = std::collections::HashMap::from([(0, vec![0; 3]), (1, vec![1; 3]), (2, vec![2])]);
|
|
|
|
let x: &[_] = if let Some(x) = map.get(&0) { x } else { &[] };
|
|
|
|
// Same code as above written using match.
|
|
|
|
let x: &[_] = match map.get(&0) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => &[],
|
|
|
|
};
|
2024-06-06 15:33:37 -05:00
|
|
|
|
|
|
|
let x: Result<String, i64> = Ok(String::new());
|
|
|
|
match x {
|
|
|
|
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => String::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let x: Result<String, i64> = Ok(String::new());
|
|
|
|
if let Ok(v) = x {
|
|
|
|
//~^ ERROR: if let can be simplified with `.unwrap_or_default()`
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
2024-03-08 15:04:31 -06:00
|
|
|
}
|
2024-03-22 06:21:35 -05:00
|
|
|
|
|
|
|
// Issue #12531
|
|
|
|
unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
|
|
|
|
match a {
|
|
|
|
// `*b` being correct depends on `a == Some(_)`
|
|
|
|
Some(_) => match *b {
|
|
|
|
Some(v) => v,
|
|
|
|
_ => 0,
|
|
|
|
},
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
2024-03-27 03:28:15 -05:00
|
|
|
|
|
|
|
const fn issue_12568(opt: Option<bool>) -> bool {
|
|
|
|
match opt {
|
|
|
|
Some(s) => s,
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 20:18:53 -05:00
|
|
|
|
|
|
|
fn issue_12569() {
|
|
|
|
let match_none_se = match 1u32.checked_div(0) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
|
|
|
println!("important");
|
|
|
|
0
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let match_some_se = match 1u32.checked_div(0) {
|
|
|
|
Some(v) => {
|
|
|
|
println!("important");
|
|
|
|
v
|
|
|
|
},
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
let iflet_else_se = if let Some(v) = 1u32.checked_div(0) {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
println!("important");
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let iflet_then_se = if let Some(v) = 1u32.checked_div(0) {
|
|
|
|
println!("important");
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
}
|