Skip to content

Commit ecbf2a5

Browse files
committed
Avoid looking regex crate up multiple times
1 parent 8305908 commit ecbf2a5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

clippy_lints/src/regex.rs

Lines changed: 8 additions & 5 deletions
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_in_crates, 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
78+
// `def_path_res_in_crates` 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));
8081
let mut resolve = |path, kind| {
81-
for id in def_path_def_ids(cx.tcx, path) {
82-
self.definitions.insert(id, kind);
82+
for res in def_path_res_in_crates(cx.tcx, regex_crates.clone(), path) {
83+
if let Some(id) = res.opt_def_id() {
84+
self.definitions.insert(id, kind);
85+
}
8386
}
8487
};
8588

clippy_utils/src/paths.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
6767
#[cfg_attr(not(unix), allow(clippy::invalid_paths))]
6868
pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"];
6969
pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
70-
pub const REGEX_BUILDER_NEW: [&str; 3] = ["regex", "RegexBuilder", "new"];
71-
pub const REGEX_BYTES_BUILDER_NEW: [&str; 4] = ["regex", "bytes", "RegexBuilder", "new"];
72-
pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "bytes", "Regex", "new"];
73-
pub const REGEX_BYTES_SET_NEW: [&str; 4] = ["regex", "bytes", "RegexSet", "new"];
74-
pub const REGEX_NEW: [&str; 3] = ["regex", "Regex", "new"];
75-
pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"];
70+
pub const REGEX_BUILDER_NEW: [&str; 2] = ["RegexBuilder", "new"];
71+
pub const REGEX_BYTES_BUILDER_NEW: [&str; 3] = ["bytes", "RegexBuilder", "new"];
72+
pub const REGEX_BYTES_NEW: [&str; 3] = ["bytes", "Regex", "new"];
73+
pub const REGEX_BYTES_SET_NEW: [&str; 3] = ["bytes", "RegexSet", "new"];
74+
pub const REGEX_NEW: [&str; 2] = ["Regex", "new"];
75+
pub const REGEX_SET_NEW: [&str; 2] = ["RegexSet", "new"];
7676
pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"];
7777
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
7878
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];

0 commit comments

Comments
 (0)