Skip to content

Commit

Permalink
report unused_import for empty reexports even it is pub
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Sep 21, 2023
1 parent e4a361a commit 544ab0b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,9 @@ pub struct Resolver<'a, 'tcx> {
/// Whether lifetime elision was successful.
lifetime_elision_allowed: FxHashSet<NodeId>,

/// These glob imports which doesn't has any reexports
empty_glob_reexports: FxHashSet<NodeId>,

/// Names of items that were stripped out via cfg with their corresponding cfg meta item.
stripped_cfg_items: Vec<StrippedCfgItem<NodeId>>,

Expand Down Expand Up @@ -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(),
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/imports/pub-reexport-empty.rs
Original file line number Diff line number Diff line change
@@ -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() {}
14 changes: 14 additions & 0 deletions tests/ui/imports/pub-reexport-empty.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 544ab0b

Please sign in to comment.