Skip to content

Commit 72bf97f

Browse files
committed
Auto merge of #88880 - cjgillot:no-krate, r=oli-obk
Rework HIR API to make invocations of the hir_crate query harder. `hir_crate` forces the recomputation of queries that depend on it. This PR aims at avoiding useless invocations of `hir_crate` by making dependent code go through `tcx.hir()`.
2 parents 730d86f + ea60b69 commit 72bf97f

13 files changed

+27
-28
lines changed

clippy_lints/src/cargo_common_metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! lint on missing cargo common metadata
22
33
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
4-
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
4+
use rustc_hir::hir_id::CRATE_HIR_ID;
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77
use rustc_span::source_map::DUMMY_SP;
@@ -77,7 +77,7 @@ fn is_empty_vec(value: &[String]) -> bool {
7777
}
7878

7979
impl LateLintPass<'_> for CargoCommonMetadata {
80-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
80+
fn check_crate(&mut self, cx: &LateContext<'_>) {
8181
if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
8282
return;
8383
}

clippy_lints/src/disallowed_method.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::fn_def_id;
33

4-
use rustc_hir::{def::Res, def_id::DefIdMap, Crate, Expr};
4+
use rustc_hir::{def::Res, def_id::DefIdMap, Expr};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77

@@ -70,7 +70,7 @@ impl DisallowedMethod {
7070
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
7171

7272
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
73-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
73+
fn check_crate(&mut self, cx: &LateContext<'_>) {
7474
for conf in &self.conf_disallowed {
7575
let (path, reason) = match conf {
7676
conf::DisallowedMethod::Simple(path) => (path, None),

clippy_lints/src/disallowed_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint;
22

33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_hir::{
5-
def::Res, def_id::DefId, Crate, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind,
5+
def::Res, def_id::DefId, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind,
66
};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -75,7 +75,7 @@ impl DisallowedType {
7575
impl_lint_pass!(DisallowedType => [DISALLOWED_TYPE]);
7676

7777
impl<'tcx> LateLintPass<'tcx> for DisallowedType {
78-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
78+
fn check_crate(&mut self, cx: &LateContext<'_>) {
7979
for path in &self.disallowed {
8080
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
8181
match clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>()) {

clippy_lints/src/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl_lint_pass!(DocMarkdown =>
212212
);
213213

214214
impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
215-
fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) {
215+
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
216216
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
217217
check_attrs(cx, &self.valid_idents, attrs);
218218
}

clippy_lints/src/feature_name.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
3-
use rustc_hir::{Crate, CRATE_HIR_ID};
3+
use rustc_hir::CRATE_HIR_ID;
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66
use rustc_span::source_map::DUMMY_SP;
@@ -110,7 +110,7 @@ fn lint(cx: &LateContext<'_>, feature: &str, substring: &str, is_prefix: bool) {
110110
}
111111

112112
impl LateLintPass<'_> for FeatureName {
113-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
113+
fn check_crate(&mut self, cx: &LateContext<'_>) {
114114
if is_lint_allowed(cx, REDUNDANT_FEATURE_NAMES, CRATE_HIR_ID)
115115
&& is_lint_allowed(cx, NEGATIVE_FEATURE_NAMES, CRATE_HIR_ID)
116116
{

clippy_lints/src/inherent_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use clippy_utils::diagnostics::span_lint_and_note;
44
use clippy_utils::{in_macro, is_lint_allowed};
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_hir::{def_id::LocalDefId, Crate, Item, ItemKind, Node};
6+
use rustc_hir::{def_id::LocalDefId, Item, ItemKind, Node};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::Span;
@@ -44,7 +44,7 @@ declare_clippy_lint! {
4444
declare_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
4545

4646
impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
47-
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Crate<'_>) {
47+
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
4848
// Map from a type to it's first impl block. Needed to distinguish generic arguments.
4949
// e.g. `Foo<Bar>` and `Foo<Baz>`
5050
let mut type_map = FxHashMap::default();

clippy_lints/src/macro_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
135135
}
136136
}
137137
#[allow(clippy::too_many_lines)]
138-
fn check_crate_post(&mut self, cx: &LateContext<'_>, _krate: &hir::Crate<'_>) {
138+
fn check_crate_post(&mut self, cx: &LateContext<'_>) {
139139
let mut used = FxHashMap::default();
140140
let mut check_dup = vec![];
141141
for (import, span) in &self.imports {

clippy_lints/src/main_recursion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::source::snippet;
33
use clippy_utils::{is_entrypoint_fn, is_no_std_crate};
44
use if_chain::if_chain;
5-
use rustc_hir::{Crate, Expr, ExprKind, QPath};
5+
use rustc_hir::{Expr, ExprKind, QPath};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
88

@@ -33,7 +33,7 @@ pub struct MainRecursion {
3333
impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
3434

3535
impl LateLintPass<'_> for MainRecursion {
36-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
36+
fn check_crate(&mut self, cx: &LateContext<'_>) {
3737
self.has_no_std_attr = is_no_std_crate(cx);
3838
}
3939

clippy_lints/src/missing_doc.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir as hir;
1212
use rustc_lint::{LateContext, LateLintPass, LintContext};
1313
use rustc_middle::ty;
1414
use rustc_session::{declare_tool_lint, impl_lint_pass};
15+
use rustc_span::def_id::CRATE_DEF_ID;
1516
use rustc_span::source_map::Span;
1617
use rustc_span::sym;
1718

@@ -78,9 +79,7 @@ impl MissingDoc {
7879
return;
7980
}
8081

81-
let has_doc = attrs
82-
.iter()
83-
.any(|a| a.doc_str().is_some());
82+
let has_doc = attrs.iter().any(|a| a.doc_str().is_some());
8483
if !has_doc {
8584
span_lint(
8685
cx,
@@ -104,9 +103,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
104103
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
105104
}
106105

107-
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
106+
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
108107
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
109-
self.check_missing_docs_attrs(cx, attrs, krate.module().inner, "the", "crate");
108+
self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
110109
}
111110

112111
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {

clippy_lints/src/missing_enforced_import_rename.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt};
22

33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_errors::Applicability;
5-
use rustc_hir::{def::Res, def_id::DefId, Crate, Item, ItemKind, UseKind};
5+
use rustc_hir::{def::Res, def_id::DefId, Item, ItemKind, UseKind};
66
use rustc_lint::{LateContext, LateLintPass, LintContext};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
88
use rustc_span::Symbol;
@@ -55,7 +55,7 @@ impl ImportRename {
5555
impl_lint_pass!(ImportRename => [MISSING_ENFORCED_IMPORT_RENAMES]);
5656

5757
impl LateLintPass<'_> for ImportRename {
58-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
58+
fn check_crate(&mut self, cx: &LateContext<'_>) {
5959
for Rename { path, rename } in &self.conf_renames {
6060
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &path.split("::").collect::<Vec<_>>()) {
6161
self.renames.insert(id, Symbol::intern(rename));

clippy_lints/src/multiple_crate_versions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use clippy_utils::diagnostics::span_lint;
44
use clippy_utils::is_lint_allowed;
55
use rustc_hir::def_id::LOCAL_CRATE;
6-
use rustc_hir::{Crate, CRATE_HIR_ID};
6+
use rustc_hir::CRATE_HIR_ID;
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::source_map::DUMMY_SP;
@@ -41,7 +41,7 @@ declare_clippy_lint! {
4141
declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]);
4242

4343
impl LateLintPass<'_> for MultipleCrateVersions {
44-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
44+
fn check_crate(&mut self, cx: &LateContext<'_>) {
4545
if is_lint_allowed(cx, MULTIPLE_CRATE_VERSIONS, CRATE_HIR_ID) {
4646
return;
4747
}

clippy_lints/src/same_name_method.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_data_structures::fx::FxHashMap;
33
use rustc_hir::def::{DefKind, Res};
4-
use rustc_hir::{Crate, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
4+
use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty::AssocKind;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -46,10 +46,10 @@ struct ExistingName {
4646
}
4747

4848
impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
49-
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'tcx>) {
49+
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
5050
let mut map = FxHashMap::<Res, ExistingName>::default();
5151

52-
for item in krate.items() {
52+
for item in cx.tcx.hir().items() {
5353
if let ItemKind::Impl(Impl {
5454
items,
5555
of_trait,

clippy_lints/src/wildcard_dependencies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
2-
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
2+
use rustc_hir::hir_id::CRATE_HIR_ID;
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55
use rustc_span::source_map::DUMMY_SP;
@@ -28,7 +28,7 @@ declare_clippy_lint! {
2828
declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
2929

3030
impl LateLintPass<'_> for WildcardDependencies {
31-
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
31+
fn check_crate(&mut self, cx: &LateContext<'_>) {
3232
if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) {
3333
return;
3434
}

0 commit comments

Comments
 (0)