Fix #[expect] for unnecessary_unwrap, panicking_unwrap

This commit is contained in:
xFrednet 2022-06-25 14:16:32 +02:00
parent bdc6ece1b6
commit 79fc2716dc
No known key found for this signature in database
GPG key ID: F5C59D0E669E5302
3 changed files with 40 additions and 22 deletions

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::higher;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{path_to_local, usage::is_potentially_mutated};
@ -251,9 +251,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
unwrappable.kind.error_variant_pattern()
};
span_lint_and_then(
span_lint_hir_and_then(
self.cx,
UNNECESSARY_UNWRAP,
expr.hir_id,
expr.span,
&format!(
"called `{}` on `{}` after checking its variant with `{}`",
@ -283,9 +284,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
},
);
} else {
span_lint_and_then(
span_lint_hir_and_then(
self.cx,
PANICKING_UNWRAP,
expr.hir_id,
expr.span,
&format!("this call to `{}()` will always panic",
method_name.ident.name),

View file

@ -1,3 +1,4 @@
#![feature(lint_reasons)]
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
@ -84,3 +85,18 @@ fn main() {
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
}
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
}
}

View file

@ -1,5 +1,5 @@
error: called `unwrap` on `x` after checking its variant with `is_some`
--> $DIR/simple_conditionals.rs:39:9
--> $DIR/simple_conditionals.rs:40:9
|
LL | if x.is_some() {
| -------------- help: try: `if let Some(..) = x`
@ -7,13 +7,13 @@ LL | x.unwrap(); // unnecessary
| ^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/simple_conditionals.rs:1:35
--> $DIR/simple_conditionals.rs:2:35
|
LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: called `expect` on `x` after checking its variant with `is_some`
--> $DIR/simple_conditionals.rs:40:9
--> $DIR/simple_conditionals.rs:41:9
|
LL | if x.is_some() {
| -------------- help: try: `if let Some(..) = x`
@ -22,7 +22,7 @@ LL | x.expect("an error message"); // unnecessary
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:42:9
--> $DIR/simple_conditionals.rs:43:9
|
LL | if x.is_some() {
| ----------- because of this check
@ -31,13 +31,13 @@ LL | x.unwrap(); // will panic
| ^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/simple_conditionals.rs:1:9
--> $DIR/simple_conditionals.rs:2:9
|
LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `expect()` will always panic
--> $DIR/simple_conditionals.rs:43:9
--> $DIR/simple_conditionals.rs:44:9
|
LL | if x.is_some() {
| ----------- because of this check
@ -46,7 +46,7 @@ LL | x.expect("an error message"); // will panic
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:46:9
--> $DIR/simple_conditionals.rs:47:9
|
LL | if x.is_none() {
| ----------- because of this check
@ -54,7 +54,7 @@ LL | x.unwrap(); // will panic
| ^^^^^^^^^^
error: called `unwrap` on `x` after checking its variant with `is_none`
--> $DIR/simple_conditionals.rs:48:9
--> $DIR/simple_conditionals.rs:49:9
|
LL | if x.is_none() {
| -------------- help: try: `if let Some(..) = x`
@ -63,7 +63,7 @@ LL | x.unwrap(); // unnecessary
| ^^^^^^^^^^
error: called `unwrap` on `x` after checking its variant with `is_some`
--> $DIR/simple_conditionals.rs:7:13
--> $DIR/simple_conditionals.rs:8:13
|
LL | if $a.is_some() {
| --------------- help: try: `if let Some(..) = x`
@ -76,7 +76,7 @@ LL | m!(x);
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
error: called `unwrap` on `x` after checking its variant with `is_ok`
--> $DIR/simple_conditionals.rs:56:9
--> $DIR/simple_conditionals.rs:57:9
|
LL | if x.is_ok() {
| ------------ help: try: `if let Ok(..) = x`
@ -84,7 +84,7 @@ LL | x.unwrap(); // unnecessary
| ^^^^^^^^^^
error: called `expect` on `x` after checking its variant with `is_ok`
--> $DIR/simple_conditionals.rs:57:9
--> $DIR/simple_conditionals.rs:58:9
|
LL | if x.is_ok() {
| ------------ help: try: `if let Ok(..) = x`
@ -93,7 +93,7 @@ LL | x.expect("an error message"); // unnecessary
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap_err()` will always panic
--> $DIR/simple_conditionals.rs:58:9
--> $DIR/simple_conditionals.rs:59:9
|
LL | if x.is_ok() {
| --------- because of this check
@ -102,7 +102,7 @@ LL | x.unwrap_err(); // will panic
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:60:9
--> $DIR/simple_conditionals.rs:61:9
|
LL | if x.is_ok() {
| --------- because of this check
@ -111,7 +111,7 @@ LL | x.unwrap(); // will panic
| ^^^^^^^^^^
error: this call to `expect()` will always panic
--> $DIR/simple_conditionals.rs:61:9
--> $DIR/simple_conditionals.rs:62:9
|
LL | if x.is_ok() {
| --------- because of this check
@ -120,7 +120,7 @@ LL | x.expect("an error message"); // will panic
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_ok`
--> $DIR/simple_conditionals.rs:62:9
--> $DIR/simple_conditionals.rs:63:9
|
LL | if x.is_ok() {
| ------------ help: try: `if let Err(..) = x`
@ -129,7 +129,7 @@ LL | x.unwrap_err(); // unnecessary
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:65:9
--> $DIR/simple_conditionals.rs:66:9
|
LL | if x.is_err() {
| ---------- because of this check
@ -137,7 +137,7 @@ LL | x.unwrap(); // will panic
| ^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_err`
--> $DIR/simple_conditionals.rs:66:9
--> $DIR/simple_conditionals.rs:67:9
|
LL | if x.is_err() {
| ------------- help: try: `if let Err(..) = x`
@ -146,7 +146,7 @@ LL | x.unwrap_err(); // unnecessary
| ^^^^^^^^^^^^^^
error: called `unwrap` on `x` after checking its variant with `is_err`
--> $DIR/simple_conditionals.rs:68:9
--> $DIR/simple_conditionals.rs:69:9
|
LL | if x.is_err() {
| ------------- help: try: `if let Ok(..) = x`
@ -155,7 +155,7 @@ LL | x.unwrap(); // unnecessary
| ^^^^^^^^^^
error: this call to `unwrap_err()` will always panic
--> $DIR/simple_conditionals.rs:69:9
--> $DIR/simple_conditionals.rs:70:9
|
LL | if x.is_err() {
| ---------- because of this check