summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/checked_unwrap
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.rs54
-rw-r--r--src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.stderr211
-rw-r--r--src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.rs15
-rw-r--r--src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.stderr31
-rw-r--r--src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.rs102
-rw-r--r--src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr167
6 files changed, 580 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.rs b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.rs
new file mode 100644
index 000000000..ec082c73b
--- /dev/null
+++ b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.rs
@@ -0,0 +1,54 @@
+#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
+
+fn test_complex_conditions() {
+ let x: Result<(), ()> = Ok(());
+ let y: Result<(), ()> = Ok(());
+ if x.is_ok() && y.is_err() {
+ x.unwrap(); // unnecessary
+ x.unwrap_err(); // will panic
+ y.unwrap(); // will panic
+ y.unwrap_err(); // unnecessary
+ } else {
+ // not statically determinable whether any of the following will always succeed or always fail:
+ x.unwrap();
+ x.unwrap_err();
+ y.unwrap();
+ y.unwrap_err();
+ }
+
+ if x.is_ok() || y.is_ok() {
+ // not statically determinable whether any of the following will always succeed or always fail:
+ x.unwrap();
+ y.unwrap();
+ } else {
+ x.unwrap(); // will panic
+ x.unwrap_err(); // unnecessary
+ y.unwrap(); // will panic
+ y.unwrap_err(); // unnecessary
+ }
+ let z: Result<(), ()> = Ok(());
+ if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ x.unwrap(); // unnecessary
+ x.unwrap_err(); // will panic
+ y.unwrap(); // will panic
+ y.unwrap_err(); // unnecessary
+ z.unwrap(); // unnecessary
+ z.unwrap_err(); // will panic
+ }
+ if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ // not statically determinable whether any of the following will always succeed or always fail:
+ x.unwrap();
+ y.unwrap();
+ z.unwrap();
+ } else {
+ x.unwrap(); // will panic
+ x.unwrap_err(); // unnecessary
+ y.unwrap(); // unnecessary
+ y.unwrap_err(); // will panic
+ z.unwrap(); // will panic
+ z.unwrap_err(); // unnecessary
+ }
+}
+
+fn main() {}
diff --git a/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.stderr b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.stderr
new file mode 100644
index 000000000..46c6f6970
--- /dev/null
+++ b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.stderr
@@ -0,0 +1,211 @@
+error: called `unwrap` on `x` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:8:9
+ |
+LL | if x.is_ok() && y.is_err() {
+ | --------- the check is happening here
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+ |
+note: the lint level is defined here
+ --> $DIR/complex_conditionals.rs:1:35
+ |
+LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
+ = help: try using `if let` or `match`
+
+error: this call to `unwrap_err()` will always panic
+ --> $DIR/complex_conditionals.rs:9:9
+ |
+LL | if x.is_ok() && y.is_err() {
+ | --------- because of this check
+LL | x.unwrap(); // unnecessary
+LL | x.unwrap_err(); // will panic
+ | ^^^^^^^^^^^^^^
+ |
+note: the lint level is defined here
+ --> $DIR/complex_conditionals.rs:1:9
+ |
+LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+ | ^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals.rs:10:9
+ |
+LL | if x.is_ok() && y.is_err() {
+ | ---------- because of this check
+...
+LL | y.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `y` after checking its variant with `is_err`
+ --> $DIR/complex_conditionals.rs:11:9
+ |
+LL | if x.is_ok() && y.is_err() {
+ | ---------- the check is happening here
+...
+LL | y.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals.rs:25:9
+ |
+LL | if x.is_ok() || y.is_ok() {
+ | --------- because of this check
+...
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `x` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:26:9
+ |
+LL | if x.is_ok() || y.is_ok() {
+ | --------- the check is happening here
+...
+LL | x.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals.rs:27:9
+ |
+LL | if x.is_ok() || y.is_ok() {
+ | --------- because of this check
+...
+LL | y.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `y` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:28:9
+ |
+LL | if x.is_ok() || y.is_ok() {
+ | --------- the check is happening here
+...
+LL | y.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: called `unwrap` on `x` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:32:9
+ |
+LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ | --------- the check is happening here
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: this call to `unwrap_err()` will always panic
+ --> $DIR/complex_conditionals.rs:33:9
+ |
+LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ | --------- because of this check
+LL | x.unwrap(); // unnecessary
+LL | x.unwrap_err(); // will panic
+ | ^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals.rs:34:9
+ |
+LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ | --------- because of this check
+...
+LL | y.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `y` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:35:9
+ |
+LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ | --------- the check is happening here
+...
+LL | y.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: called `unwrap` on `z` after checking its variant with `is_err`
+ --> $DIR/complex_conditionals.rs:36:9
+ |
+LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ | ---------- the check is happening here
+...
+LL | z.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: this call to `unwrap_err()` will always panic
+ --> $DIR/complex_conditionals.rs:37:9
+ |
+LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
+ | ---------- because of this check
+...
+LL | z.unwrap_err(); // will panic
+ | ^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals.rs:45:9
+ |
+LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ | --------- because of this check
+...
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `x` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:46:9
+ |
+LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ | --------- the check is happening here
+...
+LL | x.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: called `unwrap` on `y` after checking its variant with `is_ok`
+ --> $DIR/complex_conditionals.rs:47:9
+ |
+LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ | --------- the check is happening here
+...
+LL | y.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: this call to `unwrap_err()` will always panic
+ --> $DIR/complex_conditionals.rs:48:9
+ |
+LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ | --------- because of this check
+...
+LL | y.unwrap_err(); // will panic
+ | ^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals.rs:49:9
+ |
+LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ | ---------- because of this check
+...
+LL | z.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `z` after checking its variant with `is_err`
+ --> $DIR/complex_conditionals.rs:50:9
+ |
+LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
+ | ---------- the check is happening here
+...
+LL | z.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try using `if let` or `match`
+
+error: aborting due to 20 previous errors
+
diff --git a/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.rs b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.rs
new file mode 100644
index 000000000..043ea4148
--- /dev/null
+++ b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.rs
@@ -0,0 +1,15 @@
+#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
+
+fn test_nested() {
+ fn nested() {
+ let x = Some(());
+ if x.is_some() {
+ x.unwrap(); // unnecessary
+ } else {
+ x.unwrap(); // will panic
+ }
+ }
+}
+
+fn main() {}
diff --git a/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.stderr b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.stderr
new file mode 100644
index 000000000..542ab5330
--- /dev/null
+++ b/src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals_nested.stderr
@@ -0,0 +1,31 @@
+error: called `unwrap` on `x` after checking its variant with `is_some`
+ --> $DIR/complex_conditionals_nested.rs:8:13
+ |
+LL | if x.is_some() {
+ | -------------- help: try: `if let Some(..) = x`
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+ |
+note: the lint level is defined here
+ --> $DIR/complex_conditionals_nested.rs:1:35
+ |
+LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/complex_conditionals_nested.rs:10:13
+ |
+LL | if x.is_some() {
+ | ----------- because of this check
+...
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+ |
+note: the lint level is defined here
+ --> $DIR/complex_conditionals_nested.rs:1:9
+ |
+LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+ | ^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
diff --git a/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.rs b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.rs
new file mode 100644
index 000000000..82dce8197
--- /dev/null
+++ b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.rs
@@ -0,0 +1,102 @@
+#![feature(lint_reasons)]
+#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
+#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
+
+macro_rules! m {
+ ($a:expr) => {
+ if $a.is_some() {
+ $a.unwrap(); // unnecessary
+ }
+ };
+}
+
+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();
+ }
+ };
+}
+
+fn main() {
+ let x = Some(());
+ if x.is_some() {
+ x.unwrap(); // unnecessary
+ x.expect("an error message"); // unnecessary
+ } else {
+ x.unwrap(); // will panic
+ x.expect("an error message"); // will panic
+ }
+ if x.is_none() {
+ x.unwrap(); // will panic
+ } else {
+ x.unwrap(); // unnecessary
+ }
+ m!(x);
+ checks_in_param!(x.is_some(), x.unwrap()); // ok
+ checks_unwrap!(x, x.unwrap()); // ok
+ checks_some!(x.is_some(), x); // ok
+ let mut x: Result<(), ()> = Ok(());
+ if x.is_ok() {
+ x.unwrap(); // unnecessary
+ x.expect("an error message"); // unnecessary
+ x.unwrap_err(); // will panic
+ } else {
+ x.unwrap(); // will panic
+ x.expect("an error message"); // will panic
+ 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(());
+ // 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();
+ } else {
+ x = Ok(());
+ // 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();
+ }
+
+ 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
+ }
+}
diff --git a/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr
new file mode 100644
index 000000000..ef6882742
--- /dev/null
+++ b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr
@@ -0,0 +1,167 @@
+error: called `unwrap` on `x` after checking its variant with `is_some`
+ --> $DIR/simple_conditionals.rs:40:9
+ |
+LL | if x.is_some() {
+ | -------------- help: try: `if let Some(..) = x`
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+ |
+note: the lint level is defined here
+ --> $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:41:9
+ |
+LL | if x.is_some() {
+ | -------------- help: try: `if let Some(..) = x`
+LL | x.unwrap(); // unnecessary
+LL | x.expect("an error message"); // unnecessary
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/simple_conditionals.rs:43:9
+ |
+LL | if x.is_some() {
+ | ----------- because of this check
+...
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+ |
+note: the lint level is defined here
+ --> $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:44:9
+ |
+LL | if x.is_some() {
+ | ----------- because of this check
+...
+LL | x.expect("an error message"); // will panic
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/simple_conditionals.rs:47:9
+ |
+LL | if x.is_none() {
+ | ----------- because of this check
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap` on `x` after checking its variant with `is_none`
+ --> $DIR/simple_conditionals.rs:49:9
+ |
+LL | if x.is_none() {
+ | -------------- help: try: `if let Some(..) = x`
+...
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+
+error: called `unwrap` on `x` after checking its variant with `is_some`
+ --> $DIR/simple_conditionals.rs:8:13
+ |
+LL | if $a.is_some() {
+ | --------------- help: try: `if let Some(..) = x`
+LL | $a.unwrap(); // unnecessary
+ | ^^^^^^^^^^^
+...
+LL | m!(x);
+ | ----- in this macro invocation
+ |
+ = 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:57:9
+ |
+LL | if x.is_ok() {
+ | ------------ help: try: `if let Ok(..) = x`
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+
+error: called `expect` on `x` after checking its variant with `is_ok`
+ --> $DIR/simple_conditionals.rs:58:9
+ |
+LL | if x.is_ok() {
+ | ------------ help: try: `if let Ok(..) = x`
+LL | x.unwrap(); // unnecessary
+LL | x.expect("an error message"); // unnecessary
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: this call to `unwrap_err()` will always panic
+ --> $DIR/simple_conditionals.rs:59:9
+ |
+LL | if x.is_ok() {
+ | --------- because of this check
+...
+LL | x.unwrap_err(); // will panic
+ | ^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/simple_conditionals.rs:61:9
+ |
+LL | if x.is_ok() {
+ | --------- because of this check
+...
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: this call to `expect()` will always panic
+ --> $DIR/simple_conditionals.rs:62:9
+ |
+LL | if x.is_ok() {
+ | --------- because of this check
+...
+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:63:9
+ |
+LL | if x.is_ok() {
+ | ------------ help: try: `if let Err(..) = x`
+...
+LL | x.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+
+error: this call to `unwrap()` will always panic
+ --> $DIR/simple_conditionals.rs:66:9
+ |
+LL | if x.is_err() {
+ | ---------- because of this check
+LL | x.unwrap(); // will panic
+ | ^^^^^^^^^^
+
+error: called `unwrap_err` on `x` after checking its variant with `is_err`
+ --> $DIR/simple_conditionals.rs:67:9
+ |
+LL | if x.is_err() {
+ | ------------- help: try: `if let Err(..) = x`
+LL | x.unwrap(); // will panic
+LL | x.unwrap_err(); // unnecessary
+ | ^^^^^^^^^^^^^^
+
+error: called `unwrap` on `x` after checking its variant with `is_err`
+ --> $DIR/simple_conditionals.rs:69:9
+ |
+LL | if x.is_err() {
+ | ------------- help: try: `if let Ok(..) = x`
+...
+LL | x.unwrap(); // unnecessary
+ | ^^^^^^^^^^
+
+error: this call to `unwrap_err()` will always panic
+ --> $DIR/simple_conditionals.rs:70:9
+ |
+LL | if x.is_err() {
+ | ---------- because of this check
+...
+LL | x.unwrap_err(); // will panic
+ | ^^^^^^^^^^^^^^
+
+error: aborting due to 17 previous errors
+