2023-07-27 11:40:22 +00:00
|
|
|
//@no-rustfix: overlapping suggestions
|
2022-06-25 12:16:32 +00:00
|
|
|
#![feature(lint_reasons)]
|
2019-07-15 19:27:44 +00:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
2023-05-31 17:47:10 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::if_same_then_else,
|
|
|
|
clippy::branches_sharing_code,
|
|
|
|
clippy::unnecessary_literal_unwrap
|
|
|
|
)]
|
2019-07-15 19:27:44 +00:00
|
|
|
|
2020-02-04 20:38:26 +00:00
|
|
|
macro_rules! m {
|
|
|
|
($a:expr) => {
|
|
|
|
if $a.is_some() {
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
$a.unwrap();
|
2020-02-04 20:38:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-24 14:46:56 +00: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 19:27:44 +00:00
|
|
|
fn main() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-07-28 18:40:44 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
|
|
|
if x.is_none() {
|
2023-07-28 18:40:44 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
2020-02-04 20:38:26 +00:00
|
|
|
m!(x);
|
2023-07-28 18:40:44 +00:00
|
|
|
// ok
|
|
|
|
checks_in_param!(x.is_some(), x.unwrap());
|
|
|
|
// ok
|
|
|
|
checks_unwrap!(x, x.unwrap());
|
|
|
|
// ok
|
|
|
|
checks_some!(x.is_some(), x);
|
2019-07-15 19:27:44 +00:00
|
|
|
let mut x: Result<(), ()> = Ok(());
|
|
|
|
if x.is_ok() {
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
|
|
|
// will panic
|
|
|
|
x.unwrap_err();
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-07-28 18:40:44 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
|
|
|
// unnecessary
|
|
|
|
x.unwrap_err();
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
|
|
|
if x.is_err() {
|
2023-07-28 18:40:44 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
// unnecessary
|
|
|
|
x.unwrap_err();
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
// will panic
|
|
|
|
x.unwrap_err();
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
|
|
|
if x.is_ok() {
|
|
|
|
x = Err(());
|
2021-03-01 17:53:33 +00: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 19:27:44 +00:00
|
|
|
} else {
|
|
|
|
x = Ok(());
|
2021-03-01 17:53:33 +00: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 19:27:44 +00:00
|
|
|
}
|
2020-02-03 19:01:42 +00:00
|
|
|
|
2023-07-28 18:40:44 +00:00
|
|
|
// ok, it's a common test pattern
|
|
|
|
assert!(x.is_ok(), "{:?}", x.unwrap_err());
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
2022-06-25 12:16:32 +00:00
|
|
|
|
|
|
|
fn check_expect() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2022-06-25 12:16:32 +00:00
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
2023-07-28 18:40:44 +00:00
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
2022-06-25 12:16:32 +00:00
|
|
|
} else {
|
|
|
|
#[expect(clippy::panicking_unwrap)]
|
2023-07-28 18:40:44 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2022-06-25 12:16:32 +00:00
|
|
|
#[expect(clippy::panicking_unwrap)]
|
2023-07-28 18:40:44 +00:00
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
2022-06-25 12:16:32 +00:00
|
|
|
}
|
|
|
|
}
|