2019-07-15 19:27:44 +00:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
|
|
|
#![allow(clippy::if_same_then_else)]
|
|
|
|
|
2020-02-04 20:38:26 +00:00
|
|
|
macro_rules! m {
|
|
|
|
($a:expr) => {
|
|
|
|
if $a.is_some() {
|
|
|
|
$a.unwrap(); // unnecessary
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-15 19:27:44 +00:00
|
|
|
fn main() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
|
|
|
}
|
|
|
|
if x.is_none() {
|
|
|
|
x.unwrap(); // will panic
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
}
|
2020-02-04 20:38:26 +00:00
|
|
|
m!(x);
|
2019-07-15 19:27:44 +00:00
|
|
|
let mut x: Result<(), ()> = Ok(());
|
|
|
|
if x.is_ok() {
|
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
x.unwrap_err(); // will panic
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
|
|
|
x.unwrap_err(); // unnecessary
|
|
|
|
}
|
|
|
|
if x.is_err() {
|
|
|
|
x.unwrap(); // will panic
|
|
|
|
x.unwrap_err(); // unnecessary
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
x.unwrap_err(); // will panic
|
|
|
|
}
|
|
|
|
if x.is_ok() {
|
|
|
|
x = Err(());
|
|
|
|
x.unwrap(); // not unnecessary because of mutation of x
|
2019-08-01 05:09:57 +00:00
|
|
|
// it will always panic but the lint is not smart enough to see this (it only
|
|
|
|
// checks if conditions).
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
|
|
|
x = Ok(());
|
|
|
|
x.unwrap_err(); // not unnecessary because of mutation of x
|
2019-08-01 05:09:57 +00:00
|
|
|
// it will always panic but the lint is not smart enough to see this (it
|
|
|
|
// only checks if conditions).
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
2020-02-03 19:01:42 +00:00
|
|
|
|
|
|
|
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|