2023-07-27 06:40:22 -05:00
|
|
|
//@no-rustfix: overlapping suggestions
|
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() {
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
$a.unwrap();
|
2020-02-04 14:38:26 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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() {
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_some`
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `expect` on `x` after checking its variant with `is_some`
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `expect()` will always panic
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
|
|
|
if x.is_none() {
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_none`
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
2020-02-04 14:38:26 -06:00
|
|
|
m!(x);
|
2023-07-28 13:40:44 -05: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 14:27:44 -05:00
|
|
|
let mut x: Result<(), ()> = Ok(());
|
|
|
|
if x.is_ok() {
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_ok`
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `expect` on `x` after checking its variant with `is_ok`
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `unwrap_err()` will always panic
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `expect()` will always panic
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_ok`
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
|
|
|
if x.is_err() {
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `unwrap()` will always panic
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_err`
|
2019-07-15 14:27:44 -05:00
|
|
|
} else {
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: called `unwrap` on `x` after checking its variant with `is_err`
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: this call to `unwrap_err()` will always panic
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2023-07-28 13:40:44 -05:00
|
|
|
// ok, it's a common test pattern
|
|
|
|
assert!(x.is_ok(), "{:?}", x.unwrap_err());
|
2019-07-15 14:27:44 -05:00
|
|
|
}
|
2022-06-25 07:16:32 -05:00
|
|
|
|
2023-08-23 14:02:01 -05: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
|
|
|
|
}
|
2023-08-25 18:10:32 -05:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 14:02:01 -05:00
|
|
|
}
|
|
|
|
|
2022-06-25 07:16:32 -05:00
|
|
|
fn check_expect() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.unwrap();
|
2022-06-25 07:16:32 -05:00
|
|
|
#[expect(clippy::unnecessary_unwrap)]
|
2023-07-28 13:40:44 -05:00
|
|
|
// unnecessary
|
|
|
|
x.expect("an error message");
|
2022-06-25 07:16:32 -05:00
|
|
|
} else {
|
|
|
|
#[expect(clippy::panicking_unwrap)]
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.unwrap();
|
2022-06-25 07:16:32 -05:00
|
|
|
#[expect(clippy::panicking_unwrap)]
|
2023-07-28 13:40:44 -05:00
|
|
|
// will panic
|
|
|
|
x.expect("an error message");
|
2022-06-25 07:16:32 -05:00
|
|
|
}
|
|
|
|
}
|