From 0b1bb5fbf3a4d3e73192f95490ce7246f9fd1914 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Thu, 16 Feb 2023 13:36:51 +0000 Subject: [PATCH] Implement the lint --- clippy_lints/src/methods/mod.rs | 6 +++--- .../src/methods/unnecessary_literal_unwrap.rs | 19 +++++++------------ tests/ui/unnecessary_literal_unwrap.stderr | 11 +++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 tests/ui/unnecessary_literal_unwrap.stderr diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 6eb97227e0a..7a7d5a588b0 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3866,11 +3866,11 @@ impl Methods { Some(("or", recv, [or_arg], or_span, _)) => { or_then_unwrap::check(cx, expr, recv, or_arg, or_span); }, - Some((constructor @ "Some", _, _, _, _)) => { - unnecessary_literal_unwrap::check(cx, expr, recv, constructor); - } _ => {}, } + if let ExprKind::Call(recv, _) = recv.kind { + unnecessary_literal_unwrap::check(cx, expr, recv, name); + } unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests); }, ("unwrap_err", []) => unwrap_used::check(cx, expr, recv, true, 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 3572f4965ef..181e4a06664 100644 --- a/clippy_lints/src/methods/unnecessary_literal_unwrap.rs +++ b/clippy_lints/src/methods/unnecessary_literal_unwrap.rs @@ -1,28 +1,23 @@ -use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::ty::is_type_diagnostic_item; +use clippy_utils::{diagnostics::span_lint_and_help, is_res_lang_ctor, path_res}; use rustc_hir as hir; use rustc_lint::LateContext; -use rustc_span::sym; use super::UNNECESSARY_LITERAL_UNWRAP; -pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, constructor: &str) { - let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs(); - - let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) { - Some((UNNECESSARY_LITERAL_UNWRAP, "an `Option`", "None", "")) +pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &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 { None }; - if let Some((lint, kind, none_value, none_prefix)) = mess { - let help = format!("if this value is {none_prefix}`{none_value}`, it will panic"); - + if let Some((lint, constructor)) = mess { + let help = String::new(); span_lint_and_help( cx, lint, expr.span, - &format!("used `unwrap()` on {kind} value"), + &format!("used `{name}()` on `{constructor}` value"), None, &help, ); diff --git a/tests/ui/unnecessary_literal_unwrap.stderr b/tests/ui/unnecessary_literal_unwrap.stderr new file mode 100644 index 00000000000..5f9881b2ae3 --- /dev/null +++ b/tests/ui/unnecessary_literal_unwrap.stderr @@ -0,0 +1,11 @@ +error: used `unwrap()` on `Some` value + --> $DIR/unnecessary_literal_unwrap.rs:4:15 + | +LL | let val = Some(1).unwrap(); + | ^^^^^^^^^^^^^^^^ + | + = help: + = note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings` + +error: aborting due to previous error +