From daf61974813ba6f9e7513068bc8cce2839fb8939 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Thu, 16 Feb 2023 14:21:43 +0000 Subject: [PATCH] Implement the suggestion --- clippy_lints/src/methods/mod.rs | 8 +++---- .../src/methods/unnecessary_literal_unwrap.rs | 22 ++++++++++++++----- tests/ui/unnecessary_literal_unwrap.fixed | 11 ++++++++++ tests/ui/unnecessary_literal_unwrap.rs | 1 + tests/ui/unnecessary_literal_unwrap.stderr | 16 ++++++++++---- 5 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 tests/ui/unnecessary_literal_unwrap.fixed diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index cb82aef1ef0..a5c55a42ea5 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3664,8 +3664,8 @@ impl Methods { Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv), _ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests), } - if let ExprKind::Call(recv, _) = recv.kind { - unnecessary_literal_unwrap::check(cx, expr, recv, name); + if let ExprKind::Call(recv, [arg]) = recv.kind { + unnecessary_literal_unwrap::check(cx, expr, recv, arg, name); } }, ("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests), @@ -3873,8 +3873,8 @@ impl Methods { }, _ => {}, } - if let ExprKind::Call(recv, _) = recv.kind { - unnecessary_literal_unwrap::check(cx, expr, recv, name); + if let ExprKind::Call(recv, [arg]) = recv.kind { + unnecessary_literal_unwrap::check(cx, expr, recv, arg, name); } unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests); }, diff --git a/clippy_lints/src/methods/unnecessary_literal_unwrap.rs b/clippy_lints/src/methods/unnecessary_literal_unwrap.rs index 181e4a06664..9a82d7e58ea 100644 --- a/clippy_lints/src/methods/unnecessary_literal_unwrap.rs +++ b/clippy_lints/src/methods/unnecessary_literal_unwrap.rs @@ -1,10 +1,11 @@ -use clippy_utils::{diagnostics::span_lint_and_help, is_res_lang_ctor, path_res}; +use clippy_utils::{diagnostics::span_lint_and_then, is_res_lang_ctor, path_res}; +use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; use super::UNNECESSARY_LITERAL_UNWRAP; -pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, name: &str) { +pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, name: &str) { let mess = if is_res_lang_ctor(cx, path_res(cx, recv), hir::LangItem::OptionSome) { Some((UNNECESSARY_LITERAL_UNWRAP, "Some")) } else { @@ -12,14 +13,23 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr }; if let Some((lint, constructor)) = mess { - let help = String::new(); - span_lint_and_help( + span_lint_and_then( cx, lint, expr.span, &format!("used `{name}()` on `{constructor}` value"), - None, - &help, + |diag| { + let suggestions = vec![ + (recv.span.with_hi(arg.span.lo()), String::new()), + (expr.span.with_lo(arg.span.hi()), String::new()), + ]; + + diag.multipart_suggestion( + format!("remove the `{constructor}` and `{name}()`"), + suggestions, + Applicability::MachineApplicable, + ); + }, ); } } diff --git a/tests/ui/unnecessary_literal_unwrap.fixed b/tests/ui/unnecessary_literal_unwrap.fixed new file mode 100644 index 00000000000..8696048d332 --- /dev/null +++ b/tests/ui/unnecessary_literal_unwrap.fixed @@ -0,0 +1,11 @@ +//run-rustfix +#![warn(clippy::unnecessary_literal_unwrap)] + +fn unwrap_option() { + let val = 1; + let val = 1; +} + +fn main() { + unwrap_option(); +} diff --git a/tests/ui/unnecessary_literal_unwrap.rs b/tests/ui/unnecessary_literal_unwrap.rs index 9ae50517a3e..ae41634495b 100644 --- a/tests/ui/unnecessary_literal_unwrap.rs +++ b/tests/ui/unnecessary_literal_unwrap.rs @@ -1,3 +1,4 @@ +//run-rustfix #![warn(clippy::unnecessary_literal_unwrap)] fn unwrap_option() { diff --git a/tests/ui/unnecessary_literal_unwrap.stderr b/tests/ui/unnecessary_literal_unwrap.stderr index 414d4445cae..00d7f186fc4 100644 --- a/tests/ui/unnecessary_literal_unwrap.stderr +++ b/tests/ui/unnecessary_literal_unwrap.stderr @@ -1,19 +1,27 @@ error: used `unwrap()` on `Some` value - --> $DIR/unnecessary_literal_unwrap.rs:4:15 + --> $DIR/unnecessary_literal_unwrap.rs:5:15 | LL | let val = Some(1).unwrap(); | ^^^^^^^^^^^^^^^^ | - = help: = note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings` +help: remove the `Some` and `unwrap()` + | +LL - let val = Some(1).unwrap(); +LL + let val = 1; + | error: used `expect()` on `Some` value - --> $DIR/unnecessary_literal_unwrap.rs:5:15 + --> $DIR/unnecessary_literal_unwrap.rs:6:15 | LL | let val = Some(1).expect("this never happens"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: +help: remove the `Some` and `expect()` + | +LL - let val = Some(1).expect("this never happens"); +LL + let val = 1; + | error: aborting due to 2 previous errors