From 7ed7283e0f9de697f8d5c8c2118c4b0b8bc7d6b0 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Thu, 16 Feb 2023 15:05:56 +0000 Subject: [PATCH] Recognize `unwrap_or_else` method --- CHANGELOG.md | 1 + clippy_lints/src/methods/mod.rs | 17 +++++---- tests/ui/unnecessary_literal_unwrap.fixed | 3 ++ tests/ui/unnecessary_literal_unwrap.rs | 3 ++ tests/ui/unnecessary_literal_unwrap.stderr | 42 +++++++++++++++++----- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85fddc97047..abc35e8546f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5248,6 +5248,7 @@ Released 2018-09-13 [`unnecessary_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fold [`unnecessary_join`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_join [`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations +[`unnecessary_literal_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap [`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed [`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation [`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 5e42123a8da..4686adda683 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3893,13 +3893,16 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { ("unwrap_or_default", []) => { unnecessary_literal_unwrap::check(cx, expr, recv, name); } - ("unwrap_or_else", [u_arg]) => match method_call(recv) { - Some(("map", recv, [map_arg], _, _)) - if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {}, - _ => { - unwrap_or_else_default::check(cx, expr, recv, u_arg); - unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or"); - }, + ("unwrap_or_else", [u_arg]) => { + match method_call(recv) { + Some(("map", recv, [map_arg], _, _)) + if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {}, + _ => { + unwrap_or_else_default::check(cx, expr, recv, u_arg); + unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or"); + }, + } + unnecessary_literal_unwrap::check(cx, expr, recv, name); }, ("zip", [arg]) => { if let ExprKind::MethodCall(name, iter_recv, [], _) = recv.kind diff --git a/tests/ui/unnecessary_literal_unwrap.fixed b/tests/ui/unnecessary_literal_unwrap.fixed index 55a210bdb98..a388a7f83d2 100644 --- a/tests/ui/unnecessary_literal_unwrap.fixed +++ b/tests/ui/unnecessary_literal_unwrap.fixed @@ -1,5 +1,6 @@ //run-rustfix #![warn(clippy::unnecessary_literal_unwrap)] +#![allow(clippy::unnecessary_lazy_evaluations)] fn unwrap_option() { let _val = 1; @@ -15,11 +16,13 @@ fn unwrap_result() { fn unwrap_methods_option() { let _val = 1; let _val = 1; + let _val = 1; } fn unwrap_methods_result() { let _val = 1; let _val = 1; + let _val = 1; } fn main() { diff --git a/tests/ui/unnecessary_literal_unwrap.rs b/tests/ui/unnecessary_literal_unwrap.rs index 80f72ce3cf2..fe557957dd9 100644 --- a/tests/ui/unnecessary_literal_unwrap.rs +++ b/tests/ui/unnecessary_literal_unwrap.rs @@ -1,5 +1,6 @@ //run-rustfix #![warn(clippy::unnecessary_literal_unwrap)] +#![allow(clippy::unnecessary_lazy_evaluations)] fn unwrap_option() { let _val = Some(1).unwrap(); @@ -15,11 +16,13 @@ fn unwrap_result() { 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); } 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); } fn main() { diff --git a/tests/ui/unnecessary_literal_unwrap.stderr b/tests/ui/unnecessary_literal_unwrap.stderr index b8b5a612da1..60499fe700b 100644 --- a/tests/ui/unnecessary_literal_unwrap.stderr +++ b/tests/ui/unnecessary_literal_unwrap.stderr @@ -1,5 +1,5 @@ error: used `unwrap()` on `Some` value - --> $DIR/unnecessary_literal_unwrap.rs:5:16 + --> $DIR/unnecessary_literal_unwrap.rs:6:16 | LL | let _val = Some(1).unwrap(); | ^^^^^^^^^^^^^^^^ @@ -12,7 +12,7 @@ LL + let _val = 1; | error: used `expect()` on `Some` value - --> $DIR/unnecessary_literal_unwrap.rs:6:16 + --> $DIR/unnecessary_literal_unwrap.rs:7:16 | LL | let _val = Some(1).expect("this never happens"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -24,7 +24,7 @@ LL + let _val = 1; | error: used `unwrap()` on `Ok` value - --> $DIR/unnecessary_literal_unwrap.rs:10:16 + --> $DIR/unnecessary_literal_unwrap.rs:11:16 | LL | let _val = Ok::(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL + let _val = 1; | error: used `expect()` on `Ok` value - --> $DIR/unnecessary_literal_unwrap.rs:11:16 + --> $DIR/unnecessary_literal_unwrap.rs:12:16 | LL | let _val = Ok::(1).expect("this never happens"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL + let _val = 1; | error: used `unwrap_or()` on `Some` value - --> $DIR/unnecessary_literal_unwrap.rs:16:16 + --> $DIR/unnecessary_literal_unwrap.rs:17:16 | LL | let _val = Some(1).unwrap_or(2); | ^^^^^^^^^^^^^^^^^^^^ @@ -60,7 +60,7 @@ LL + let _val = 1; | error: used `unwrap_or_default()` on `Some` value - --> $DIR/unnecessary_literal_unwrap.rs:17:16 + --> $DIR/unnecessary_literal_unwrap.rs:18:16 | LL | let _val = Some(1).unwrap_or_default(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -71,8 +71,20 @@ LL - let _val = Some(1).unwrap_or_default(); LL + let _val = 1; | +error: used `unwrap_or_else()` on `Some` value + --> $DIR/unnecessary_literal_unwrap.rs:19:16 + | +LL | let _val = Some(1).unwrap_or_else(|| _val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Some` and `unwrap_or_else()` + | +LL - let _val = Some(1).unwrap_or_else(|| _val); +LL + let _val = 1; + | + error: used `unwrap_or()` on `Ok` value - --> $DIR/unnecessary_literal_unwrap.rs:21:16 + --> $DIR/unnecessary_literal_unwrap.rs:23:16 | LL | let _val = Ok::(1).unwrap_or(2); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -84,7 +96,7 @@ LL + let _val = 1; | error: used `unwrap_or_default()` on `Ok` value - --> $DIR/unnecessary_literal_unwrap.rs:22:16 + --> $DIR/unnecessary_literal_unwrap.rs:24:16 | LL | let _val = Ok::(1).unwrap_or_default(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -95,5 +107,17 @@ LL - let _val = Ok::(1).unwrap_or_default(); LL + let _val = 1; | -error: aborting due to 8 previous errors +error: used `unwrap_or_else()` on `Ok` value + --> $DIR/unnecessary_literal_unwrap.rs:25:16 + | +LL | let _val = Ok::(1).unwrap_or_else(|()| _val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the `Ok` and `unwrap_or_else()` + | +LL - let _val = Ok::(1).unwrap_or_else(|()| _val); +LL + let _val = 1; + | + +error: aborting due to 10 previous errors