Skip to content

Commit 326315b

Browse files
authored
Rollup merge of #97609 - Elliot-Roberts:unused-trait-refactor, r=cjgillot
Iterate over `maybe_unused_trait_imports` when checking dead trait imports Closes #96873 r? `@cjgillot` Some questions, if you have time: - Is there a way to shorten the `rustc_data_structures::fx::FxIndexSet` path in the query declaration? I wasn't sure where to put a `use`. - Was returning by reference from the query the right choice here? - How would I go about evaluating the importance of the `is_dummy()` call in `check_crate`? I don't see failing tests when I comment it out. Should I just try to determine whether dummy spans can ever be put into `maybe_unused_trait_imports`? - Am I doing anything silly with the various ID types? - Is that `let-else` with `unreachable!()` bad? (i.e is there a better idiom? Would `panic!("<explanation>")` be better?) - If I want to evaluate the perf of using a `Vec` as mentioned in #96873, is the best way to use the CI or is it feasible locally? Thanks :)
2 parents 3a8e713 + 76c6845 commit 326315b

File tree

6 files changed

+29
-45
lines changed

6 files changed

+29
-45
lines changed

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1728,8 +1728,8 @@ rustc_queries! {
17281728
query upvars_mentioned(def_id: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
17291729
desc { |tcx| "collecting upvars mentioned in `{}`", tcx.def_path_str(def_id) }
17301730
}
1731-
query maybe_unused_trait_import(def_id: LocalDefId) -> bool {
1732-
desc { |tcx| "maybe_unused_trait_import for `{}`", tcx.def_path_str(def_id.to_def_id()) }
1731+
query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> {
1732+
desc { "fetching potentially unused trait imports" }
17331733
}
17341734
query maybe_unused_extern_crates(_: ()) -> &'tcx [(LocalDefId, Span)] {
17351735
desc { "looking up all possibly unused extern crates" }

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2893,8 +2893,8 @@ pub fn provide(providers: &mut ty::query::Providers) {
28932893
assert_eq!(id, LOCAL_CRATE);
28942894
tcx.crate_name
28952895
};
2896-
providers.maybe_unused_trait_import =
2897-
|tcx, id| tcx.resolutions(()).maybe_unused_trait_imports.contains(&id);
2896+
providers.maybe_unused_trait_imports =
2897+
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
28982898
providers.maybe_unused_extern_crates =
28992899
|tcx, ()| &tcx.resolutions(()).maybe_unused_extern_crates[..];
29002900
providers.names_imported_by_glob_use = |tcx, id| {

compiler/rustc_middle/src/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub use generics::*;
2828
use rustc_ast as ast;
2929
use rustc_attr as attr;
3030
use rustc_data_structures::fingerprint::Fingerprint;
31-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
31+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3232
use rustc_data_structures::intern::{Interned, WithStableHash};
3333
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3434
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
@@ -138,7 +138,7 @@ pub struct ResolverOutputs {
138138
pub has_pub_restricted: bool,
139139
pub access_levels: AccessLevels,
140140
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
141-
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
141+
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
142142
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
143143
pub reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
144144
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,

compiler/rustc_middle/src/ty/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, T
3737
use rustc_ast as ast;
3838
use rustc_ast::expand::allocator::AllocatorKind;
3939
use rustc_attr as attr;
40-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
40+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
4141
use rustc_data_structures::steal::Steal;
4242
use rustc_data_structures::svh::Svh;
4343
use rustc_data_structures::sync::Lrc;

compiler/rustc_resolve/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_ast::node_id::NodeMap;
2828
use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
2929
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
3030
use rustc_ast_lowering::{LifetimeRes, ResolverAstLowering};
31-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
31+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3232
use rustc_data_structures::intern::Interned;
3333
use rustc_data_structures::sync::Lrc;
3434
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
@@ -941,7 +941,7 @@ pub struct Resolver<'a> {
941941
visibilities: FxHashMap<LocalDefId, ty::Visibility>,
942942
has_pub_restricted: bool,
943943
used_imports: FxHashSet<NodeId>,
944-
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
944+
maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
945945
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
946946

947947
/// Privacy errors are delayed until the end in order to deduplicate them.

compiler/rustc_typeck/src/check_unused.rs

+20-36
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,32 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
1616
used_trait_imports.extend(imports.iter());
1717
}
1818

19-
for id in tcx.hir().items() {
20-
if matches!(tcx.def_kind(id.def_id), DefKind::Use) {
21-
if tcx.visibility(id.def_id).is_public() {
22-
continue;
23-
}
24-
let item = tcx.hir().item(id);
25-
if item.span.is_dummy() {
26-
continue;
27-
}
28-
if let hir::ItemKind::Use(path, _) = item.kind {
29-
check_import(tcx, &mut used_trait_imports, item.item_id(), path.span);
30-
}
19+
for &id in tcx.maybe_unused_trait_imports(()) {
20+
debug_assert_eq!(tcx.def_kind(id), DefKind::Use);
21+
if tcx.visibility(id).is_public() {
22+
continue;
23+
}
24+
if used_trait_imports.contains(&id) {
25+
continue;
3126
}
27+
let item = tcx.hir().expect_item(id);
28+
if item.span.is_dummy() {
29+
continue;
30+
}
31+
let hir::ItemKind::Use(path, _) = item.kind else { unreachable!() };
32+
tcx.struct_span_lint_hir(lint::builtin::UNUSED_IMPORTS, item.hir_id(), path.span, |lint| {
33+
let msg = if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(path.span) {
34+
format!("unused import: `{}`", snippet)
35+
} else {
36+
"unused import".to_owned()
37+
};
38+
lint.build(&msg).emit();
39+
});
3240
}
3341

3442
unused_crates_lint(tcx);
3543
}
3644

37-
fn check_import<'tcx>(
38-
tcx: TyCtxt<'tcx>,
39-
used_trait_imports: &mut FxHashSet<LocalDefId>,
40-
item_id: hir::ItemId,
41-
span: Span,
42-
) {
43-
if !tcx.maybe_unused_trait_import(item_id.def_id) {
44-
return;
45-
}
46-
47-
if used_trait_imports.contains(&item_id.def_id) {
48-
return;
49-
}
50-
51-
tcx.struct_span_lint_hir(lint::builtin::UNUSED_IMPORTS, item_id.hir_id(), span, |lint| {
52-
let msg = if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) {
53-
format!("unused import: `{}`", snippet)
54-
} else {
55-
"unused import".to_owned()
56-
};
57-
lint.build(&msg).emit();
58-
});
59-
}
60-
6145
fn unused_crates_lint(tcx: TyCtxt<'_>) {
6246
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
6347

0 commit comments

Comments
 (0)