Skip to content

Commit 3961ef5

Browse files
authored
Rollup merge of #109487 - GuillaumeGomez:move-useless-reexport-check, r=petrochenkov
Move useless_anynous_reexport lint into unused_imports As mentioned in #109003, this check should have been merged with `unused_imports` in the start. r? `@petrochenkov`
2 parents dddede4 + e03b13c commit 3961ef5

11 files changed

+70
-141
lines changed

compiler/rustc_lint/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,3 @@ lint_opaque_hidden_inferred_bound = opaque type `{$ty}` does not satisfy its ass
508508
.specifically = this associated type bound is unsatisfied for `{$proj_ty}`
509509
510510
lint_opaque_hidden_inferred_bound_sugg = add this bound
511-
512-
lint_useless_anonymous_reexport = useless anonymous re-export
513-
.note = only anonymous re-exports of traits are useful, this is {$article} `{$desc}`

compiler/rustc_lint/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ mod opaque_hidden_inferred_bound;
7474
mod pass_by_value;
7575
mod passes;
7676
mod redundant_semicolon;
77-
mod reexports;
7877
mod traits;
7978
mod types;
8079
mod unused;
@@ -112,7 +111,6 @@ use noop_method_call::*;
112111
use opaque_hidden_inferred_bound::*;
113112
use pass_by_value::*;
114113
use redundant_semicolon::*;
115-
use reexports::*;
116114
use traits::*;
117115
use types::*;
118116
use unused::*;
@@ -244,7 +242,6 @@ late_lint_methods!(
244242
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
245243
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
246244
MapUnitFn: MapUnitFn,
247-
UselessAnonymousReexport: UselessAnonymousReexport,
248245
]
249246
]
250247
);

compiler/rustc_lint/src/lints.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1528,11 +1528,3 @@ pub struct UnusedAllocationDiag;
15281528
#[derive(LintDiagnostic)]
15291529
#[diag(lint_unused_allocation_mut)]
15301530
pub struct UnusedAllocationMutDiag;
1531-
1532-
#[derive(LintDiagnostic)]
1533-
#[diag(lint_useless_anonymous_reexport)]
1534-
#[note]
1535-
pub struct UselessAnonymousReexportDiag {
1536-
pub article: &'static str,
1537-
pub desc: &'static str,
1538-
}

compiler/rustc_lint/src/reexports.rs

-82
This file was deleted.

compiler/rustc_resolve/src/check_unused.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ use rustc_ast::visit::{self, Visitor};
3232
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
3333
use rustc_data_structures::unord::UnordSet;
3434
use rustc_errors::{pluralize, MultiSpan};
35+
use rustc_hir::def::{DefKind, Res};
3536
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS};
3637
use rustc_session::lint::BuiltinLintDiagnostics;
37-
use rustc_span::symbol::Ident;
38+
use rustc_span::symbol::{kw, Ident};
3839
use rustc_span::{Span, DUMMY_SP};
3940

4041
struct UnusedImport<'a> {
@@ -58,6 +59,7 @@ struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
5859
base_use_tree: Option<&'a ast::UseTree>,
5960
base_id: ast::NodeId,
6061
item_span: Span,
62+
base_use_is_pub: bool,
6163
}
6264

6365
struct ExternCrateToLint {
@@ -110,6 +112,35 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
110112
unused: Default::default(),
111113
})
112114
}
115+
116+
fn check_import_as_underscore(&mut self, item: &ast::UseTree, id: ast::NodeId) {
117+
match item.kind {
118+
ast::UseTreeKind::Simple(Some(ident)) => {
119+
if ident.name == kw::Underscore
120+
&& !self
121+
.r
122+
.import_res_map
123+
.get(&id)
124+
.map(|per_ns| {
125+
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
126+
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
127+
})
128+
})
129+
.unwrap_or(false)
130+
{
131+
self.unused_import(self.base_id).add(id);
132+
}
133+
}
134+
ast::UseTreeKind::Nested(ref items) => self.check_imports_as_underscore(items),
135+
_ => {}
136+
}
137+
}
138+
139+
fn check_imports_as_underscore(&mut self, items: &[(ast::UseTree, ast::NodeId)]) {
140+
for (item, id) in items {
141+
self.check_import_as_underscore(item, *id);
142+
}
143+
}
113144
}
114145

115146
impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
@@ -119,7 +150,8 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
119150
// whether they're used or not. Also ignore imports with a dummy span
120151
// because this means that they were generated in some fashion by the
121152
// compiler and we don't need to consider them.
122-
ast::ItemKind::Use(..) if item.vis.kind.is_pub() || item.span.is_dummy() => return,
153+
ast::ItemKind::Use(..) if item.span.is_dummy() => return,
154+
ast::ItemKind::Use(..) => self.base_use_is_pub = item.vis.kind.is_pub(),
123155
ast::ItemKind::ExternCrate(orig_name) => {
124156
self.extern_crate_items.push(ExternCrateToLint {
125157
id: item.id,
@@ -146,6 +178,11 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
146178
self.base_use_tree = Some(use_tree);
147179
}
148180

181+
if self.base_use_is_pub {
182+
self.check_import_as_underscore(use_tree, id);
183+
return;
184+
}
185+
149186
if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
150187
if items.is_empty() {
151188
self.unused_import(self.base_id).add(id);
@@ -300,6 +337,7 @@ impl Resolver<'_, '_> {
300337
base_use_tree: None,
301338
base_id: ast::DUMMY_NODE_ID,
302339
item_span: DUMMY_SP,
340+
base_use_is_pub: false,
303341
};
304342
visit::walk_crate(&mut visitor, krate);
305343

tests/ui/imports/issue-99695-b.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
2+
#![allow(unused, nonstandard_style)]
33
mod m {
44

55
mod p {

tests/ui/imports/issue-99695-b.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
2+
#![allow(unused, nonstandard_style)]
33
mod m {
44

55
mod p {

tests/ui/imports/issue-99695.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
2+
#![allow(unused, nonstandard_style)]
33
mod m {
44
#[macro_export]
55
macro_rules! nu {

tests/ui/imports/issue-99695.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
2+
#![allow(unused, nonstandard_style)]
33
mod m {
44
#[macro_export]
55
macro_rules! nu {

tests/ui/lint/anonymous-reexport.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(useless_anonymous_reexport)]
1+
#![deny(unused_imports)]
22
#![crate_type = "rlib"]
33

44
mod my_mod {
@@ -9,13 +9,11 @@ mod my_mod {
99
}
1010

1111
pub use self::my_mod::Foo as _;
12-
pub use self::my_mod::TyFoo as _;
13-
pub use self::my_mod::Bar as _; //~ ERROR
14-
pub use self::my_mod::TyBar as _; //~ ERROR
15-
pub use self::my_mod::{Bar as _}; //~ ERROR
16-
pub use self::my_mod::{Bar as _, Foo as _}; //~ ERROR
17-
pub use self::my_mod::{Bar as _, TyBar as _};
18-
//~^ ERROR
19-
//~| ERROR
12+
pub use self::my_mod::TyFoo as _; //~ ERROR unused import
13+
pub use self::my_mod::Bar as _; //~ ERROR unused import
14+
pub use self::my_mod::TyBar as _; //~ ERROR unused import
15+
pub use self::my_mod::{Bar as _}; //~ ERROR unused import
16+
pub use self::my_mod::{Bar as _, Foo as _}; //~ ERROR unused import
17+
pub use self::my_mod::{Bar as _, TyBar as _}; //~ ERROR unused imports
2018
#[allow(unused_imports)]
2119
use self::my_mod::TyBar as _;
+19-30
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,44 @@
1-
error: useless anonymous re-export
2-
--> $DIR/anonymous-reexport.rs:13:1
1+
error: unused import: `self::my_mod::TyFoo as _`
2+
--> $DIR/anonymous-reexport.rs:12:9
33
|
4-
LL | pub use self::my_mod::Bar as _;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | pub use self::my_mod::TyFoo as _;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: only anonymous re-exports of traits are useful, this is a `struct`
87
note: the lint level is defined here
98
--> $DIR/anonymous-reexport.rs:1:9
109
|
11-
LL | #![deny(useless_anonymous_reexport)]
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | #![deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
1312

14-
error: useless anonymous re-export
15-
--> $DIR/anonymous-reexport.rs:14:1
13+
error: unused import: `self::my_mod::Bar as _`
14+
--> $DIR/anonymous-reexport.rs:13:9
1615
|
17-
LL | pub use self::my_mod::TyBar as _;
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | pub use self::my_mod::Bar as _;
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: unused import: `self::my_mod::TyBar as _`
20+
--> $DIR/anonymous-reexport.rs:14:9
1921
|
20-
= note: only anonymous re-exports of traits are useful, this is a `type alias`
22+
LL | pub use self::my_mod::TyBar as _;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^
2124

22-
error: useless anonymous re-export
25+
error: unused import: `Bar as _`
2326
--> $DIR/anonymous-reexport.rs:15:24
2427
|
2528
LL | pub use self::my_mod::{Bar as _};
2629
| ^^^^^^^^
27-
|
28-
= note: only anonymous re-exports of traits are useful, this is a `struct`
2930

30-
error: useless anonymous re-export
31+
error: unused import: `Bar as _`
3132
--> $DIR/anonymous-reexport.rs:16:24
3233
|
3334
LL | pub use self::my_mod::{Bar as _, Foo as _};
3435
| ^^^^^^^^
35-
|
36-
= note: only anonymous re-exports of traits are useful, this is a `struct`
3736

38-
error: useless anonymous re-export
37+
error: unused imports: `Bar as _`, `TyBar as _`
3938
--> $DIR/anonymous-reexport.rs:17:24
4039
|
4140
LL | pub use self::my_mod::{Bar as _, TyBar as _};
42-
| ^^^^^^^^
43-
|
44-
= note: only anonymous re-exports of traits are useful, this is a `struct`
45-
46-
error: useless anonymous re-export
47-
--> $DIR/anonymous-reexport.rs:17:34
48-
|
49-
LL | pub use self::my_mod::{Bar as _, TyBar as _};
50-
| ^^^^^^^^^^
51-
|
52-
= note: only anonymous re-exports of traits are useful, this is a `type alias`
41+
| ^^^^^^^^ ^^^^^^^^^^
5342

5443
error: aborting due to 6 previous errors
5544

0 commit comments

Comments
 (0)