]> git.lizzy.rs Git - rust.git/blobdiff - clippy_utils/src/lib.rs
Auto merge of #6823 - Jarcho:diagnostic_items, r=phansch
[rust.git] / clippy_utils / src / lib.rs
index 0395079901cb3e809c2882376815c10ba5af6073..daeced6bad483ab9f57208c71154a3b277a4ac63 100644 (file)
@@ -63,8 +63,9 @@
 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
 use rustc_hir::Node;
 use rustc_hir::{
-    def, Arm, Block, Body, Constness, Crate, Expr, ExprKind, FnDecl, HirId, ImplItem, ImplItemKind, Item, ItemKind,
-    MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind, TraitRef, TyKind, Unsafety,
+    def, Arm, Block, Body, Constness, Crate, Expr, ExprKind, FnDecl, GenericArgs, HirId, ImplItem, ImplItemKind, Item,
+    ItemKind, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind, TraitRef, TyKind,
+    Unsafety,
 };
 use rustc_infer::infer::TyCtxtInferExt;
 use rustc_lint::{LateContext, Level, Lint, LintContext};
@@ -272,6 +273,27 @@ pub fn last_path_segment<'tcx>(path: &QPath<'tcx>) -> &'tcx PathSegment<'tcx> {
     }
 }
 
+pub fn get_qpath_generics(path: &QPath<'tcx>) -> Option<&'tcx GenericArgs<'tcx>> {
+    match path {
+        QPath::Resolved(_, p) => p.segments.last().and_then(|s| s.args),
+        QPath::TypeRelative(_, s) => s.args,
+        QPath::LangItem(..) => None,
+    }
+}
+
+pub fn get_qpath_generic_tys(path: &QPath<'tcx>) -> impl Iterator<Item = &'tcx hir::Ty<'tcx>> {
+    get_qpath_generics(path)
+        .map_or([].as_ref(), |a| a.args)
+        .iter()
+        .filter_map(|a| {
+            if let hir::GenericArg::Type(ty) = a {
+                Some(ty)
+            } else {
+                None
+            }
+        })
+}
+
 pub fn single_segment_path<'tcx>(path: &QPath<'tcx>) -> Option<&'tcx PathSegment<'tcx>> {
     match *path {
         QPath::Resolved(_, ref path) => path.segments.get(0),