]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/clippy/clippy_lints/src/needless_question_mark.rs
Rollup merge of #104901 - krtab:filetype_compare, r=the8472
[rust.git] / src / tools / clippy / clippy_lints / src / needless_question_mark.rs
index 8f85b00596c019f248a290b2087771977257e377..97c8cfbd3eb7a195cda0aa3b4a1caa7ffdfcde40 100644 (file)
@@ -1,11 +1,12 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
-use clippy_utils::is_lang_ctor;
+use clippy_utils::path_res;
 use clippy_utils::source::snippet;
 use if_chain::if_chain;
 use rustc_errors::Applicability;
-use rustc_hir::LangItem::{OptionSome, ResultOk};
+use rustc_hir::def::{DefKind, Res};
 use rustc_hir::{AsyncGeneratorKind, Block, Body, Expr, ExprKind, GeneratorKind, LangItem, MatchSource, QPath};
 use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::ty::DefIdTree;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 declare_clippy_lint! {
@@ -112,11 +113,12 @@ fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
 
 fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
     if_chain! {
-        if let ExprKind::Call(path, [arg]) = &expr.kind;
-        if let ExprKind::Path(ref qpath) = &path.kind;
-        let sugg_remove = if is_lang_ctor(cx, qpath, OptionSome) {
+        if let ExprKind::Call(path, [arg]) = expr.kind;
+        if let Res::Def(DefKind::Ctor(..), ctor_id) = path_res(cx, path);
+        if let Some(variant_id) = cx.tcx.opt_parent(ctor_id);
+        let sugg_remove = if cx.tcx.lang_items().option_some_variant() == Some(variant_id) {
             "Some()"
-        } else if is_lang_ctor(cx, qpath, ResultOk) {
+        } else if cx.tcx.lang_items().result_ok_variant() == Some(variant_id) {
             "Ok()"
         } else {
             return;
@@ -134,7 +136,7 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
                 NEEDLESS_QUESTION_MARK,
                 expr.span,
                 "question mark operator is useless here",
-                &format!("try removing question mark and `{}`", sugg_remove),
+                &format!("try removing question mark and `{sugg_remove}`"),
                 format!("{}", snippet(cx, inner_expr.span, r#""...""#)),
                 Applicability::MachineApplicable,
             );