From 544ab0bcb6e02bdf239031c0f88abe92bb1c2692 Mon Sep 17 00:00:00 2001 From: bohan Date: Thu, 21 Sep 2023 22:05:20 +0800 Subject: [PATCH] report `unused_import` for empty reexports even it is pub --- compiler/rustc_resolve/src/check_unused.rs | 2 +- compiler/rustc_resolve/src/imports.rs | 4 ++++ compiler/rustc_resolve/src/lib.rs | 5 +++++ tests/ui/imports/pub-reexport-empty.rs | 18 ++++++++++++++++++ tests/ui/imports/pub-reexport-empty.stderr | 14 ++++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/ui/imports/pub-reexport-empty.rs create mode 100644 tests/ui/imports/pub-reexport-empty.stderr diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 7dbbd4c34ea7d..f49119e3efdac 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -173,7 +173,7 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> { self.base_use_tree = Some(use_tree); } - if self.base_use_is_pub { + if self.base_use_is_pub && !self.r.empty_glob_reexports.contains(&id) { self.check_import_as_underscore(use_tree, id); return; } diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index d271519a8a385..e1c31630b53d7 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -987,6 +987,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { candidates: None, }); } + + if self.resolutions(module).borrow().is_empty() { + self.empty_glob_reexports.insert(id); + } } if !is_prelude && let Some(max_vis) = max_vis.get() diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 949c6ab5ac0c1..c591bd35a7c31 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1108,6 +1108,9 @@ pub struct Resolver<'a, 'tcx> { /// Whether lifetime elision was successful. lifetime_elision_allowed: FxHashSet, + /// These glob imports which doesn't has any reexports + empty_glob_reexports: FxHashSet, + /// Names of items that were stripped out via cfg with their corresponding cfg meta item. stripped_cfg_items: Vec>, @@ -1336,6 +1339,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { determined_imports: Vec::new(), indeterminate_imports: Vec::new(), + empty_glob_reexports: Default::default(), + pat_span_map: Default::default(), partial_res_map: Default::default(), import_res_map: Default::default(), diff --git a/tests/ui/imports/pub-reexport-empty.rs b/tests/ui/imports/pub-reexport-empty.rs new file mode 100644 index 0000000000000..fd5f02f073c04 --- /dev/null +++ b/tests/ui/imports/pub-reexport-empty.rs @@ -0,0 +1,18 @@ +#![deny(unused_imports)] + +mod a {} + +pub use a::*; +//~^ ERROR: unused import: `a::*` + +mod b { + mod c { + #[derive(Clone)] + pub struct D; + } + pub use self::c::*; // don't show unused import lint +} + +pub use b::*; // don't show unused import lint + +fn main() {} diff --git a/tests/ui/imports/pub-reexport-empty.stderr b/tests/ui/imports/pub-reexport-empty.stderr new file mode 100644 index 0000000000000..1f422deb78c5f --- /dev/null +++ b/tests/ui/imports/pub-reexport-empty.stderr @@ -0,0 +1,14 @@ +error: unused import: `a::*` + --> $DIR/pub-reexport-empty.rs:5:9 + | +LL | pub use a::*; + | ^^^^ + | +note: the lint level is defined here + --> $DIR/pub-reexport-empty.rs:1:9 + | +LL | #![deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to previous error +