2022-06-30 03:50:09 -05:00
|
|
|
#![feature(lint_reasons)]
|
2019-07-15 14:27:44 -05:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
2023-07-02 07:35:19 -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-05-11 13:23:47 -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-09-08 09:31:47 -05:00
|
|
|
x.expect("an error message"); // unnecessary
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
2021-09-08 09:31:47 -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-05-11 13:23:47 -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-09-08 09:31:47 -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-09-08 09:31:47 -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-12 08:30:50 -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-12 08:30:50 -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-30 03:50:09 -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
|
|
|
|
}
|
|
|
|
}
|