From 6cf138e9b63e63b913b5b567d9a1cb6870a8738f Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Thu, 16 Feb 2023 23:14:20 +0000 Subject: [PATCH] Add more tests --- tests/ui/unnecessary_literal_unwrap.fixed | 4 +- tests/ui/unnecessary_literal_unwrap.rs | 8 +- tests/ui/unnecessary_literal_unwrap.stderr | 12 +- .../unnecessary_literal_unwrap_unfixable.rs | 50 ++++- ...nnecessary_literal_unwrap_unfixable.stderr | 210 +++++++++++++++++- 5 files changed, 268 insertions(+), 16 deletions(-) diff --git a/tests/ui/unnecessary_literal_unwrap.fixed b/tests/ui/unnecessary_literal_unwrap.fixed index d622c984c52..41c4dfda47f 100644 --- a/tests/ui/unnecessary_literal_unwrap.fixed +++ b/tests/ui/unnecessary_literal_unwrap.fixed @@ -1,7 +1,7 @@ -//run-rustfix +//@run-rustfix #![warn(clippy::unnecessary_literal_unwrap)] -#![allow(clippy::unnecessary_lazy_evaluations)] #![allow(unreachable_code)] +#![allow(clippy::unnecessary_lazy_evaluations)] fn unwrap_option_some() { let _val = 1; diff --git a/tests/ui/unnecessary_literal_unwrap.rs b/tests/ui/unnecessary_literal_unwrap.rs index 926265a3471..f15c063fe54 100644 --- a/tests/ui/unnecessary_literal_unwrap.rs +++ b/tests/ui/unnecessary_literal_unwrap.rs @@ -1,7 +1,7 @@ -//run-rustfix +//@run-rustfix #![warn(clippy::unnecessary_literal_unwrap)] -#![allow(clippy::unnecessary_lazy_evaluations)] #![allow(unreachable_code)] +#![allow(clippy::unnecessary_lazy_evaluations)] fn unwrap_option_some() { let _val = Some(1).unwrap(); @@ -30,13 +30,13 @@ fn unwrap_result_err() { fn unwrap_methods_option() { let _val = Some(1).unwrap_or(2); let _val = Some(1).unwrap_or_default(); - let _val = Some(1).unwrap_or_else(|| _val); + let _val = Some(1).unwrap_or_else(|| 2); } fn unwrap_methods_result() { let _val = Ok::(1).unwrap_or(2); let _val = Ok::(1).unwrap_or_default(); - let _val = Ok::(1).unwrap_or_else(|()| _val); + let _val = Ok::(1).unwrap_or_else(|_| 2); } fn main() { diff --git a/tests/ui/unnecessary_literal_unwrap.stderr b/tests/ui/unnecessary_literal_unwrap.stderr index 1d23bb22bdd..cf057d7fe5a 100644 --- a/tests/ui/unnecessary_literal_unwrap.stderr +++ b/tests/ui/unnecessary_literal_unwrap.stderr @@ -159,12 +159,12 @@ LL + let _val = 1; error: used `unwrap_or_else()` on `Some` value --> $DIR/unnecessary_literal_unwrap.rs:33:16 | -LL | let _val = Some(1).unwrap_or_else(|| _val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _val = Some(1).unwrap_or_else(|| 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: remove the `Some` and `unwrap_or_else()` | -LL - let _val = Some(1).unwrap_or_else(|| _val); +LL - let _val = Some(1).unwrap_or_else(|| 2); LL + let _val = 1; | @@ -195,12 +195,12 @@ LL + let _val = 1; error: used `unwrap_or_else()` on `Ok` value --> $DIR/unnecessary_literal_unwrap.rs:39:16 | -LL | let _val = Ok::(1).unwrap_or_else(|()| _val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _val = Ok::(1).unwrap_or_else(|_| 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: remove the `Ok` and `unwrap_or_else()` | -LL - let _val = Ok::(1).unwrap_or_else(|()| _val); +LL - let _val = Ok::(1).unwrap_or_else(|_| 2); LL + let _val = 1; | diff --git a/tests/ui/unnecessary_literal_unwrap_unfixable.rs b/tests/ui/unnecessary_literal_unwrap_unfixable.rs index ac3a84934af..9ccf6b21447 100644 --- a/tests/ui/unnecessary_literal_unwrap_unfixable.rs +++ b/tests/ui/unnecessary_literal_unwrap_unfixable.rs @@ -1,6 +1,54 @@ #![warn(clippy::unnecessary_literal_unwrap)] +#![allow(clippy::unnecessary_lazy_evaluations)] +#![allow(unreachable_code)] -fn main() { +fn unwrap_option_some() { let val = Some(1); let _val2 = val.unwrap(); + let _val2 = val.expect("this never happens"); +} + +fn unwrap_option_none() { + let val = None::; + val.unwrap(); + val.expect("this always happens"); +} + +fn unwrap_result_ok() { + let val = Ok::(1); + let _val2 = val.unwrap(); + let _val2 = val.expect("this never happens"); + val.unwrap_err(); + val.expect_err("this always happens"); +} + +fn unwrap_result_err() { + let val = Err::<(), usize>(1); + let _val2 = val.unwrap_err(); + let _val2 = val.expect_err("this never happens"); + val.unwrap(); + val.expect("this always happens"); +} + +fn unwrap_methods_option() { + let val = Some(1); + let _val2 = val.unwrap_or(2); + let _val2 = val.unwrap_or_default(); + let _val2 = val.unwrap_or_else(|| 2); +} + +fn unwrap_methods_result() { + let val = Ok::(1); + let _val2 = val.unwrap_or(2); + let _val2 = val.unwrap_or_default(); + let _val2 = val.unwrap_or_else(|_| 2); +} + +fn main() { + unwrap_option_some(); + unwrap_option_none(); + unwrap_result_ok(); + unwrap_result_err(); + unwrap_methods_option(); + unwrap_methods_result(); } diff --git a/tests/ui/unnecessary_literal_unwrap_unfixable.stderr b/tests/ui/unnecessary_literal_unwrap_unfixable.stderr index abc789ccda4..7931deeca7f 100644 --- a/tests/ui/unnecessary_literal_unwrap_unfixable.stderr +++ b/tests/ui/unnecessary_literal_unwrap_unfixable.stderr @@ -1,15 +1,219 @@ error: used `unwrap()` on `Some` value - --> $DIR/unnecessary_literal_unwrap_unfixable.rs:5:17 + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:7:17 | LL | let _val2 = val.unwrap(); | ^^^^^^^^^^^^ | help: remove the `Some` and `unwrap()` - --> $DIR/unnecessary_literal_unwrap_unfixable.rs:4:15 + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:6:15 | LL | let val = Some(1); | ^^^^^^^ = note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings` -error: aborting due to previous error +error: used `expect()` on `Some` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:8:17 + | +LL | let _val2 = val.expect("this never happens"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Some` and `expect()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:6:15 + | +LL | let val = Some(1); + | ^^^^^^^ + +error: used `unwrap()` on `None` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:13:5 + | +LL | val.unwrap(); + | ^^^^^^^^^^^^ + | +help: remove the `None` and `unwrap()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:12:15 + | +LL | let val = None::; + | ^^^^^^^^^^^^^ + +error: used `expect()` on `None` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:14:5 + | +LL | val.expect("this always happens"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `None` and `expect()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:12:15 + | +LL | let val = None::; + | ^^^^^^^^^^^^^ + +error: used `unwrap()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:19:17 + | +LL | let _val2 = val.unwrap(); + | ^^^^^^^^^^^^ + | +help: remove the `Ok` and `unwrap()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:18:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: used `expect()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:20:17 + | +LL | let _val2 = val.expect("this never happens"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `expect()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:18:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: used `unwrap_err()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:21:5 + | +LL | val.unwrap_err(); + | ^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `unwrap_err()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:18:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: used `expect_err()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:22:5 + | +LL | val.expect_err("this always happens"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `expect_err()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:18:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: used `unwrap_err()` on `Err` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:27:17 + | +LL | let _val2 = val.unwrap_err(); + | ^^^^^^^^^^^^^^^^ + | +help: remove the `Err` and `unwrap_err()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:26:15 + | +LL | let val = Err::<(), usize>(1); + | ^^^^^^^^^^^^^^^^^^^ + +error: used `expect_err()` on `Err` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:28:17 + | +LL | let _val2 = val.expect_err("this never happens"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Err` and `expect_err()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:26:15 + | +LL | let val = Err::<(), usize>(1); + | ^^^^^^^^^^^^^^^^^^^ + +error: used `unwrap()` on `Err` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:29:5 + | +LL | val.unwrap(); + | ^^^^^^^^^^^^ + | +help: remove the `Err` and `unwrap()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:26:15 + | +LL | let val = Err::<(), usize>(1); + | ^^^^^^^^^^^^^^^^^^^ + +error: used `expect()` on `Err` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:30:5 + | +LL | val.expect("this always happens"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Err` and `expect()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:26:15 + | +LL | let val = Err::<(), usize>(1); + | ^^^^^^^^^^^^^^^^^^^ + +error: used `unwrap_or()` on `Some` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:35:17 + | +LL | let _val2 = val.unwrap_or(2); + | ^^^^^^^^^^^^^^^^ + | +help: remove the `Some` and `unwrap_or()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:34:15 + | +LL | let val = Some(1); + | ^^^^^^^ + +error: used `unwrap_or_default()` on `Some` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:36:17 + | +LL | let _val2 = val.unwrap_or_default(); + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Some` and `unwrap_or_default()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:34:15 + | +LL | let val = Some(1); + | ^^^^^^^ + +error: used `unwrap_or_else()` on `Some` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:37:17 + | +LL | let _val2 = val.unwrap_or_else(|| 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Some` and `unwrap_or_else()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:34:15 + | +LL | let val = Some(1); + | ^^^^^^^ + +error: used `unwrap_or()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:42:17 + | +LL | let _val2 = val.unwrap_or(2); + | ^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `unwrap_or()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:41:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: used `unwrap_or_default()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:43:17 + | +LL | let _val2 = val.unwrap_or_default(); + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `unwrap_or_default()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:41:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: used `unwrap_or_else()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:44:17 + | +LL | let _val2 = val.unwrap_or_else(|_| 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `unwrap_or_else()` + --> $DIR/unnecessary_literal_unwrap_unfixable.rs:41:15 + | +LL | let val = Ok::(1); + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 18 previous errors