Skip to content

Commit a4d4321

Browse files
authored
Optimize incompatible_msrv lint (#15422)
This limits repeated lookups in pre-checks (to determine if a MSRV should be checked), especially when those require locking up an interner: - The `core` crate is looked up once when creating the lint, instead of comparing the crate name with `sym::core` at every check. - `span.ctxt().outer_expn_data()` is lookup up only once. changelog: none r? blyxyas
2 parents 925710d + 26911aa commit a4d4321

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

clippy_lints/src/incompatible_msrv.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::{self as hir, AmbigArg, Expr, ExprKind, HirId, QPath};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_session::impl_lint_pass;
12-
use rustc_span::def_id::DefId;
12+
use rustc_span::def_id::{CrateNum, DefId};
1313
use rustc_span::{ExpnKind, Span, sym};
1414

1515
declare_clippy_lint! {
@@ -83,16 +83,22 @@ pub struct IncompatibleMsrv {
8383
msrv: Msrv,
8484
availability_cache: FxHashMap<(DefId, bool), Availability>,
8585
check_in_tests: bool,
86+
core_crate: Option<CrateNum>,
8687
}
8788

8889
impl_lint_pass!(IncompatibleMsrv => [INCOMPATIBLE_MSRV]);
8990

9091
impl IncompatibleMsrv {
91-
pub fn new(conf: &'static Conf) -> Self {
92+
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
9293
Self {
9394
msrv: conf.msrv,
9495
availability_cache: FxHashMap::default(),
9596
check_in_tests: conf.check_incompatible_msrv_in_tests,
97+
core_crate: tcx
98+
.crates(())
99+
.iter()
100+
.find(|krate| tcx.crate_name(**krate) == sym::core)
101+
.copied(),
96102
}
97103
}
98104

@@ -140,23 +146,16 @@ impl IncompatibleMsrv {
140146
// We don't check local items since their MSRV is supposed to always be valid.
141147
return;
142148
}
143-
if let ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) = span.ctxt().outer_expn_data().kind {
149+
let expn_data = span.ctxt().outer_expn_data();
150+
if let ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) = expn_data.kind {
144151
// Desugared expressions get to cheat and stability is ignored.
145152
// Intentionally not using `.from_expansion()`, since we do still care about macro expansions
146153
return;
147154
}
148-
149155
// Functions coming from `core` while expanding a macro such as `assert*!()` get to cheat too: the
150156
// macros may have existed prior to the checked MSRV, but their expansion with a recent compiler
151157
// might use recent functions or methods. Compiling with an older compiler would not use those.
152-
if span.from_expansion()
153-
&& cx.tcx.crate_name(def_id.krate) == sym::core
154-
&& span
155-
.ctxt()
156-
.outer_expn_data()
157-
.macro_def_id
158-
.is_some_and(|def_id| cx.tcx.crate_name(def_id.krate) == sym::core)
159-
{
158+
if Some(def_id.krate) == self.core_crate && expn_data.macro_def_id.map(|did| did.krate) == self.core_crate {
160159
return;
161160
}
162161

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
797797
store.register_late_pass(|_| Box::<unconditional_recursion::UnconditionalRecursion>::default());
798798
store.register_late_pass(move |_| Box::new(pub_underscore_fields::PubUnderscoreFields::new(conf)));
799799
store.register_late_pass(move |_| Box::new(missing_const_for_thread_local::MissingConstForThreadLocal::new(conf)));
800-
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(conf)));
800+
store.register_late_pass(move |tcx| Box::new(incompatible_msrv::IncompatibleMsrv::new(tcx, conf)));
801801
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
802802
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
803803
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(conf)));

0 commit comments

Comments
 (0)