about summary refs log tree commit diff
path: root/clippy_utils
diff options
context:
space:
mode:
authorJade <software@lfcode.ca>2021-08-05 20:08:52 -0700
committerJade <software@lfcode.ca>2021-08-10 14:40:26 -0700
commitc78cc7ac1f1e89cd2e12d29374e1c5e6110d9217 (patch)
tree79e1682ad046a6ac67c4c2cdc24bb7939e51e910 /clippy_utils
parent11ef04728c90aa6105b20c681e3c12f231ba4f12 (diff)
downloadrust-c78cc7ac1f1e89cd2e12d29374e1c5e6110d9217.tar.gz
rust-c78cc7ac1f1e89cd2e12d29374e1c5e6110d9217.zip
Add is_trait_item, refactor or_fun_call and unwrap_or_else_default
Diffstat (limited to 'clippy_utils')
-rw-r--r--clippy_utils/src/lib.rs19
-rw-r--r--clippy_utils/src/ty.rs16
2 files changed, 19 insertions, 16 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index 59f878f8b20..da9560f8ccf 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -326,6 +326,25 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
         .map_or(false, |did| is_diag_trait_item(cx, did, diag_item))
 }
 
+/// Checks if the given expression is a path referring an item on the trait
+/// that is marked with the given diagnostic item.
+///
+/// For checking method call expressions instead of path expressions, use
+/// [`is_trait_method`].
+///
+/// For example, to find if an expression like `u64::default` refers to an item
+/// of the trait `Default`, which is marked `#[rustc_diagnostic_item = "Default"]`,
+/// a `diag_item` of `sym::Default` should be used.
+pub fn is_trait_item(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool {
+    if let hir::ExprKind::Path(ref qpath) = expr.kind {
+        cx.qpath_res(qpath, expr.hir_id)
+            .opt_def_id()
+            .map_or(false, |def_id| is_diag_trait_item(cx, def_id, diag_item))
+    } else {
+        false
+    }
+}
+
 pub fn last_path_segment<'tcx>(path: &QPath<'tcx>) -> &'tcx PathSegment<'tcx> {
     match *path {
         QPath::Resolved(_, path) => path.segments.last().expect("A path must have at least one segment"),
diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs
index bc31bea8b9f..536d0f006e3 100644
--- a/clippy_utils/src/ty.rs
+++ b/clippy_utils/src/ty.rs
@@ -2,7 +2,6 @@
 
 #![allow(clippy::module_name_repetitions)]
 
-use hir::{HirId, QPath};
 use rustc_ast::ast::Mutability;
 use rustc_data_structures::fx::FxHashMap;
 use rustc_hir as hir;
@@ -137,21 +136,6 @@ pub fn implements_trait<'tcx>(
     })
 }
 
-/// Gets the trait that a path targets. For example `<SomeTy as Trait>::a` would return the
-/// [`DefId`] for `Trait`.
-///
-/// `cx` must be in a body.
-pub fn qpath_target_trait<'tcx>(cx: &LateContext<'tcx>, qpath: &QPath<'_>, expr_id: HirId) -> Option<DefId> {
-    let method_res = cx.typeck_results().qpath_res(qpath, expr_id);
-    let method_id = match method_res {
-        hir::def::Res::Def(_kind, id) => Some(id),
-        _ => None,
-    };
-    let method_id = method_id?;
-
-    cx.tcx.trait_of_item(method_id)
-}
-
 /// Checks whether this type implements `Drop`.
 pub fn has_drop<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
     match ty.ty_adt_def() {