Skip to content

Commit 91581f6

Browse files
committed
Resolve primitive impls in clippy_utils::path_to_res
1 parent ae01c4a commit 91581f6

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

clippy_utils/src/lib.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ use rustc_data_structures::fx::FxHashMap;
7070
use rustc_data_structures::unhash::UnhashMap;
7171
use rustc_hir as hir;
7272
use rustc_hir::def::{DefKind, Res};
73-
use rustc_hir::def_id::DefId;
73+
use rustc_hir::def_id::{CrateNum, DefId};
7474
use rustc_hir::hir_id::{HirIdMap, HirIdSet};
7575
use rustc_hir::intravisit::{walk_expr, ErasedMap, FnKind, NestedVisitorMap, Visitor};
7676
use rustc_hir::itemlikevisit::ItemLikeVisitor;
7777
use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk};
7878
use rustc_hir::{
79-
def, Arm, BindingAnnotation, Block, BlockCheckMode, Body, Constness, Destination, Expr, ExprKind, FnDecl,
80-
ForeignItem, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, Local,
81-
MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem,
82-
TraitItemKind, TraitRef, TyKind, UnOp,
79+
def, lang_items, Arm, BindingAnnotation, Block, BlockCheckMode, Body, Constness, Destination, Expr, ExprKind,
80+
FnDecl, ForeignItem, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, Local,
81+
MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Target,
82+
TraitItem, TraitItemKind, TraitRef, TyKind, UnOp,
8383
};
8484
use rustc_lint::{LateContext, Level, Lint, LintContext};
8585
use rustc_middle::hir::exports::Export;
@@ -525,18 +525,34 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
525525
.iter()
526526
.find(|item| item.ident.name.as_str() == name)
527527
}
528+
fn find_primitive(tcx: TyCtxt<'_>, name: &str) -> Option<DefId> {
529+
if let Some(&(index, Target::Impl)) = lang_items::ITEM_REFS.get(&Symbol::intern(name)) {
530+
tcx.lang_items().items()[index]
531+
} else {
532+
None
533+
}
534+
}
535+
fn find_crate(tcx: TyCtxt<'_>, name: &str) -> Option<DefId> {
536+
tcx.crates(())
537+
.iter()
538+
.find(|&&num| tcx.crate_name(num).as_str() == name)
539+
.map(CrateNum::as_def_id)
540+
}
528541

529-
let (krate, first, path) = match *path {
530-
[krate, first, ref path @ ..] => (krate, first, path),
542+
let (base, first, path) = match *path {
543+
[base, first, ref path @ ..] => (base, first, path),
531544
[primitive] => {
532545
return PrimTy::from_name(Symbol::intern(primitive)).map_or(Res::Err, Res::PrimTy);
533546
},
534547
_ => return Res::Err,
535548
};
536549
let tcx = cx.tcx;
537-
let crates = tcx.crates(());
538-
let krate = try_res!(crates.iter().find(|&&num| tcx.crate_name(num).as_str() == krate));
539-
let first = try_res!(item_child_by_name(tcx, krate.as_def_id(), first));
550+
let first = try_res!(
551+
find_primitive(tcx, base)
552+
.or_else(|| find_crate(tcx, base))
553+
.and_then(|id| item_child_by_name(tcx, id, first))
554+
);
555+
540556
let last = path
541557
.iter()
542558
.copied()

0 commit comments

Comments
 (0)