2019-07-15 14:27:44 -05:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
2021-04-01 11:30:47 -05:00
|
|
|
#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
|
2019-07-15 14:27:44 -05:00
|
|
|
|
2020-02-04 14:38:26 -06:00
|
|
|
macro_rules! m {
|
|
|
|
($a:expr) => {
|
|
|
|
if $a.is_some() {
|
|
|
|
$a.unwrap(); // unnecessary
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:46:56 -05:00
|
|
|
macro_rules! checks_in_param {
|
|
|
|
($a:expr, $b:expr) => {
|
|
|
|
if $a {
|
|
|
|
$b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! checks_unwrap {
|
|
|
|
($a:expr, $b:expr) => {
|
|
|
|
if $a.is_some() {
|
|
|
|
$b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! checks_some {
|
|
|
|
($a:expr, $b:expr) => {
|
|
|
|
if $a {
|
|
|
|
$b.unwrap();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-15 14:27:44 -05: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 14:38:26 -06:00
|
|
|
m!(x);
|
2020-04-24 09:46:56 -05:00
|
|
|
checks_in_param!(x.is_some(), x.unwrap()); // ok
|
|
|
|
checks_unwrap!(x, x.unwrap()); // ok
|
|
|
|
checks_some!(x.is_some(), x); // ok
|
2019-07-15 14:27:44 -05: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(());
|
2021-03-01 11:53:33 -06:00
|
|
|
// not unnecessary because of mutation of x
|
|
|
|
// it will always panic but the lint is not smart enough to see this (it only
|
|
|
|
// checks if conditions).
|
|
|
|
x.unwrap();
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
|
|
|
x = Ok(());
|
2021-03-01 11:53:33 -06:00
|
|
|
// not unnecessary because of mutation of x
|
|
|
|
// it will always panic but the lint is not smart enough to see this (it
|
|
|
|
// only checks if conditions).
|
|
|
|
x.unwrap_err();
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
2020-02-03 13:01:42 -06:00
|
|
|
|
|
|
|
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|