2022-06-25 07:16:32 -05:00
|
|
|
#![feature(lint_reasons)]
|
2019-07-15 14:27:44 -05:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
2023-05-31 12:47:10 -05:00
|
|
|
#![allow(
|
|
|
|
clippy::if_same_then_else,
|
|
|
|
clippy::branches_sharing_code,
|
|
|
|
clippy::unnecessary_literal_unwrap
|
|
|
|
)]
|
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
|
2021-08-18 08:56:25 -05:00
|
|
|
x.expect("an error message"); // unnecessary
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
2021-08-18 08:56:25 -05:00
|
|
|
x.expect("an error message"); // will panic
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
|
|
|
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
|
2021-08-18 08:56:25 -05:00
|
|
|
x.expect("an error message"); // unnecessary
|
2019-07-15 14:27:44 -05:00
|
|
|
x.unwrap_err(); // will panic
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
2021-08-18 08:56:25 -05:00
|
|
|
x.expect("an error message"); // will panic
|
2019-07-15 14:27:44 -05:00
|
|
|
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
|
|
|
}
|
2022-06-25 07:16:32 -05:00
|
|
|
|
|
|
|
fn check_expect() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
|
|
|
x.expect("an error message"); // unnecessary
|
|
|
|
} else {
|
|
|
|
#[expect(clippy::panicking_unwrap)]
|
|
|
|
x.unwrap(); // will panic
|
|
|
|
#[expect(clippy::panicking_unwrap)]
|
|
|
|
x.expect("an error message"); // will panic
|
|
|
|
}
|
|
|
|
}
|