2023-08-24 19:32:12 +00:00
|
|
|
//@no-rustfix: overlapping suggestions
|
2019-07-15 19:27:44 +00:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
2023-07-02 12:35:19 +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-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
$a.unwrap();
|
2020-02-04 20:38:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:23:47 +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-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_some`
|
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
|
|
|
//~^ ERROR: called `expect` on `x` after checking its variant with `is_some`
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-08-24 19:32:12 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
|
|
|
//~^ ERROR: this call to `expect()` will always panic
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
|
|
|
if x.is_none() {
|
2023-08-24 19:32:12 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_none`
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
2020-02-04 20:38:26 +00:00
|
|
|
m!(x);
|
2023-08-24 19:32:12 +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-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_ok`
|
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
|
|
|
//~^ ERROR: called `expect` on `x` after checking its variant with `is_ok`
|
|
|
|
// will panic
|
|
|
|
x.unwrap_err();
|
|
|
|
//~^ ERROR: this call to `unwrap_err()` will always panic
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-08-24 19:32:12 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
|
|
|
//~^ ERROR: this call to `expect()` will always panic
|
|
|
|
// unnecessary
|
|
|
|
x.unwrap_err();
|
|
|
|
//~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_ok`
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
|
|
|
if x.is_err() {
|
2023-08-24 19:32:12 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
// unnecessary
|
|
|
|
x.unwrap_err();
|
|
|
|
//~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_err`
|
2019-07-15 19:27:44 +00:00
|
|
|
} else {
|
2023-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_err`
|
|
|
|
// will panic
|
|
|
|
x.unwrap_err();
|
|
|
|
//~^ ERROR: this call to `unwrap_err()` will always panic
|
2019-07-15 19:27:44 +00:00
|
|
|
}
|
|
|
|
if x.is_ok() {
|
|
|
|
x = Err(());
|
2021-03-12 14:30:50 +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-12 14:30:50 +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-08-24 19:32:12 +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-30 08:50:09 +00:00
|
|
|
|
2023-09-12 16:13:53 +00:00
|
|
|
fn issue11371() {
|
|
|
|
let option = Some(());
|
|
|
|
|
|
|
|
if option.is_some() {
|
|
|
|
option.as_ref().unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some`
|
|
|
|
} else {
|
|
|
|
option.as_ref().unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = Ok::<(), ()>(());
|
|
|
|
|
|
|
|
if result.is_ok() {
|
|
|
|
result.as_ref().unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok`
|
|
|
|
} else {
|
|
|
|
result.as_ref().unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut option = Some(());
|
|
|
|
if option.is_some() {
|
|
|
|
option.as_mut().unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some`
|
|
|
|
} else {
|
|
|
|
option.as_mut().unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut result = Ok::<(), ()>(());
|
|
|
|
if result.is_ok() {
|
|
|
|
result.as_mut().unwrap();
|
|
|
|
//~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok`
|
|
|
|
} else {
|
|
|
|
result.as_mut().unwrap();
|
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should not lint. Statics are, at the time of writing, not linted on anyway,
|
|
|
|
// but if at some point they are supported by this lint, it should correctly see that
|
|
|
|
// `X` is being mutated and not suggest `if let Some(..) = X {}`
|
|
|
|
static mut X: Option<i32> = Some(123);
|
|
|
|
unsafe {
|
|
|
|
if X.is_some() {
|
|
|
|
X = None;
|
|
|
|
X.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 08:50:09 +00:00
|
|
|
fn check_expect() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
2023-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2022-06-30 08:50:09 +00:00
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
2023-08-24 19:32:12 +00:00
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
2022-06-30 08:50:09 +00:00
|
|
|
} else {
|
|
|
|
#[expect(clippy::panicking_unwrap)]
|
2023-08-24 19:32:12 +00:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2022-06-30 08:50:09 +00:00
|
|
|
#[expect(clippy::panicking_unwrap)]
|
2023-08-24 19:32:12 +00:00
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
2022-06-30 08:50:09 +00:00
|
|
|
}
|
|
|
|
}
|