diff options
| author | Cameron Steffen <cam.steffen94@gmail.com> | 2022-01-17 13:39:45 -0600 |
|---|---|---|
| committer | Cameron Steffen <cam.steffen94@gmail.com> | 2022-01-28 15:45:41 -0600 |
| commit | 145d7fc5298cec0995bdc4e71d09d275fcbcdcdb (patch) | |
| tree | 4241fc16418c071b63459cbf5dd8b7e47c276820 | |
| parent | 20781f195d205dcc3fa244a659dfc5b3547a87d3 (diff) | |
| download | rust-145d7fc5298cec0995bdc4e71d09d275fcbcdcdb.tar.gz rust-145d7fc5298cec0995bdc4e71d09d275fcbcdcdb.zip | |
Factor out get_qpath_generics
| -rw-r--r-- | clippy_utils/src/lib.rs | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 64634c64602..82d50238666 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -77,9 +77,9 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk}; use rustc_hir::{ def, lang_items, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Constness, Destination, Expr, - ExprKind, FnDecl, ForeignItem, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, - Local, MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, - Target, TraitItem, TraitItemKind, TraitRef, TyKind, UnOp, + ExprKind, FnDecl, ForeignItem, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, Local, + MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Target, + TraitItem, TraitItemKind, TraitRef, TyKind, UnOp, }; use rustc_lint::{LateContext, Level, Lint, LintContext}; use rustc_middle::hir::place::PlaceBase; @@ -360,24 +360,14 @@ pub fn last_path_segment<'tcx>(path: &QPath<'tcx>) -> &'tcx PathSegment<'tcx> { } } -pub fn get_qpath_generics<'tcx>(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 qpath_generic_tys<'tcx>(path: &QPath<'tcx>) -> impl Iterator<Item = &'tcx hir::Ty<'tcx>> { - get_qpath_generics(path) - .map_or([].as_ref(), |a| a.args) +pub fn qpath_generic_tys<'tcx>(qpath: &QPath<'tcx>) -> impl Iterator<Item = &'tcx hir::Ty<'tcx>> { + last_path_segment(qpath) + .args + .map_or(&[][..], |a| a.args) .iter() - .filter_map(|a| { - if let hir::GenericArg::Type(ty) = a { - Some(ty) - } else { - None - } + .filter_map(|a| match a { + hir::GenericArg::Type(ty) => Some(ty), + _ => None, }) } |
