Skip to content

Commit 1f0e19a

Browse files
committed
resolve: only report unused import for no-reexports lint
1 parent 62d9034 commit 1f0e19a

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

compiler/rustc_resolve/src/check_unused.rs

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// - `check_crate` finally emits the diagnostics based on the data generated
2424
// in the last step
2525

26+
use crate::fluent_generated as fluent;
2627
use crate::imports::ImportKind;
2728
use crate::module_to_string;
2829
use crate::Resolver;
@@ -487,4 +488,20 @@ impl Resolver<'_, '_> {
487488
);
488489
}
489490
}
491+
492+
pub(crate) fn check_no_reexports(&mut self) {
493+
for import in &self.no_reexports_imports {
494+
let ImportKind::Glob { id, .. } = import.kind else {
495+
unreachable!();
496+
};
497+
if !import.used.get() {
498+
self.lint_buffer.buffer_lint(
499+
UNUSED_IMPORTS,
500+
id,
501+
import.span,
502+
fluent::resolve_glob_import_doesnt_reexport,
503+
);
504+
}
505+
}
506+
}
490507
}

compiler/rustc_resolve/src/imports.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::errors::{
88
ItemsInTraitsAreNotImportable,
99
};
1010
use crate::Determinacy::{self, *};
11-
use crate::{fluent_generated as fluent, Namespace::*};
11+
use crate::Namespace::*;
1212
use crate::{module_to_string, names_to_string, ImportSuggestion};
1313
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
1414
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
@@ -965,7 +965,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
965965
id,
966966
..
967967
} => (source, target, source_bindings, target_bindings, type_ns_only, id),
968-
ImportKind::Glob { is_prelude, ref max_vis, id } => {
968+
ImportKind::Glob { is_prelude, ref max_vis, .. } => {
969969
if import.module_path.len() <= 1 {
970970
// HACK(eddyb) `lint_if_path_starts_with_module` needs at least
971971
// 2 segments, so the `resolve_path` above won't trigger it.
@@ -992,7 +992,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
992992
&& let Some(max_vis) = max_vis.get()
993993
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
994994
{
995-
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, id, import.span, fluent::resolve_glob_import_doesnt_reexport);
995+
self.no_reexports_imports.push(import);
996996
}
997997
return None;
998998
}

compiler/rustc_resolve/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,8 @@ pub struct Resolver<'a, 'tcx> {
954954
/// All non-determined imports.
955955
indeterminate_imports: Vec<Import<'a>>,
956956

957+
no_reexports_imports: Vec<Import<'a>>,
958+
957959
// Spans for local variables found during pattern resolution.
958960
// Used for suggestions during error reporting.
959961
pat_span_map: NodeMap<Span>,
@@ -1336,6 +1338,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13361338
determined_imports: Vec::new(),
13371339
indeterminate_imports: Vec::new(),
13381340

1341+
no_reexports_imports: Vec::new(),
1342+
13391343
pat_span_map: Default::default(),
13401344
partial_res_map: Default::default(),
13411345
import_res_map: Default::default(),
@@ -1605,6 +1609,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16051609
self.tcx.sess.time("late_resolve_crate", || self.late_resolve_crate(krate));
16061610
self.tcx.sess.time("resolve_main", || self.resolve_main());
16071611
self.tcx.sess.time("resolve_check_unused", || self.check_unused(krate));
1612+
self.tcx.sess.time("resolve_check_no_reexports", || self.check_no_reexports());
16081613
self.tcx.sess.time("resolve_report_errors", || self.report_errors(krate));
16091614
self.tcx
16101615
.sess
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
// https://github.com/rust-lang/rust/issues/115966
4+
5+
#![deny(unused_imports)]
6+
7+
mod a {
8+
mod b {
9+
pub(crate) const C: &str = "123";
10+
}
11+
pub use self::b::*;
12+
}
13+
14+
fn main() {
15+
let _ = a::C;
16+
}

0 commit comments

Comments
 (0)