Skip to content

Commit 8ab744e

Browse files
committed
Auto merge of #13431 - GnomedDev:split-def_path_res, r=y21
Split def_path_res into two parts `def_path_res` previously had two jobs: 1. looking up the crates to find the path in 2. looking up path in said crates This splits that job up into two functions, keeping `def_path_res` as an adapter between the both, to avoid repeating the first step when repeatedly looking up items in the same crate. changelog: none
2 parents b7ab258 + 6b34c8d commit 8ab744e

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

clippy_lints/src/regex.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::Display;
33
use clippy_utils::consts::{ConstEvalCtxt, Constant};
44
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
55
use clippy_utils::source::SpanRangeExt;
6-
use clippy_utils::{def_path_def_ids, path_def_id, paths};
6+
use clippy_utils::{def_path_res_with_base, find_crates, path_def_id, paths};
77
use rustc_ast::ast::{LitKind, StrStyle};
88
use rustc_hir::def_id::DefIdMap;
99
use rustc_hir::{BorrowKind, Expr, ExprKind};
@@ -75,11 +75,14 @@ impl<'tcx> LateLintPass<'tcx> for Regex {
7575
// We don't use `match_def_path` here because that relies on matching the exact path, which changed
7676
// between regex 1.8 and 1.9
7777
//
78-
// `def_path_def_ids` will resolve through re-exports but is relatively heavy, so we only perform
79-
// the operation once and store the results
80-
let mut resolve = |path, kind| {
81-
for id in def_path_def_ids(cx.tcx, path) {
82-
self.definitions.insert(id, kind);
78+
// `def_path_res_with_base` will resolve through re-exports but is relatively heavy, so we only
79+
// perform the operation once and store the results
80+
let regex_crates = find_crates(cx.tcx, sym!(regex));
81+
let mut resolve = |path: &[&str], kind: RegexKind| {
82+
for res in def_path_res_with_base(cx.tcx, regex_crates.clone(), &path[1..]) {
83+
if let Some(id) = res.opt_def_id() {
84+
self.definitions.insert(id, kind);
85+
}
8386
}
8487
};
8588

clippy_utils/src/lib.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,17 @@ fn item_children_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec<Re
671671
}
672672
}
673673

674+
/// Finds the crates called `name`, may be multiple due to multiple major versions.
675+
pub fn find_crates(tcx: TyCtxt<'_>, name: Symbol) -> Vec<Res> {
676+
tcx.crates(())
677+
.iter()
678+
.copied()
679+
.filter(move |&num| tcx.crate_name(num) == name)
680+
.map(CrateNum::as_def_id)
681+
.map(|id| Res::Def(tcx.def_kind(id), id))
682+
.collect()
683+
}
684+
674685
/// Resolves a def path like `std::vec::Vec`.
675686
///
676687
/// Can return multiple resolutions when there are multiple versions of the same crate, e.g.
@@ -681,15 +692,7 @@ fn item_children_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec<Re
681692
///
682693
/// This function is expensive and should be used sparingly.
683694
pub fn def_path_res(tcx: TyCtxt<'_>, path: &[&str]) -> Vec<Res> {
684-
fn find_crates(tcx: TyCtxt<'_>, name: Symbol) -> impl Iterator<Item = DefId> + '_ {
685-
tcx.crates(())
686-
.iter()
687-
.copied()
688-
.filter(move |&num| tcx.crate_name(num) == name)
689-
.map(CrateNum::as_def_id)
690-
}
691-
692-
let (base, mut path) = match *path {
695+
let (base, path) = match *path {
693696
[primitive] => {
694697
return vec![PrimTy::from_name(Symbol::intern(primitive)).map_or(Res::Err, Res::PrimTy)];
695698
},
@@ -705,18 +708,25 @@ pub fn def_path_res(tcx: TyCtxt<'_>, path: &[&str]) -> Vec<Res> {
705708
None
706709
};
707710

708-
let starts = find_primitive_impls(tcx, base)
709-
.chain(find_crates(tcx, base_sym))
711+
let crates = find_primitive_impls(tcx, base)
710712
.chain(local_crate)
711-
.map(|id| Res::Def(tcx.def_kind(id), id));
713+
.map(|id| Res::Def(tcx.def_kind(id), id))
714+
.chain(find_crates(tcx, base_sym))
715+
.collect();
712716

713-
let mut resolutions: Vec<Res> = starts.collect();
717+
def_path_res_with_base(tcx, crates, path)
718+
}
714719

720+
/// Resolves a def path like `vec::Vec` with the base `std`.
721+
///
722+
/// This is lighter than [`def_path_res`], and should be called with [`find_crates`] looking up
723+
/// items from the same crate repeatedly, although should still be used sparingly.
724+
pub fn def_path_res_with_base(tcx: TyCtxt<'_>, mut base: Vec<Res>, mut path: &[&str]) -> Vec<Res> {
715725
while let [segment, rest @ ..] = path {
716726
path = rest;
717727
let segment = Symbol::intern(segment);
718728

719-
resolutions = resolutions
729+
base = base
720730
.into_iter()
721731
.filter_map(|res| res.opt_def_id())
722732
.flat_map(|def_id| {
@@ -735,7 +745,7 @@ pub fn def_path_res(tcx: TyCtxt<'_>, path: &[&str]) -> Vec<Res> {
735745
.collect();
736746
}
737747

738-
resolutions
748+
base
739749
}
740750

741751
/// Resolves a def path like `std::vec::Vec` to its [`DefId`]s, see [`def_path_res`].

0 commit comments

Comments
 (0)