Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move useless_anynous_reexport lint into unused_imports #109487

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,3 @@ lint_opaque_hidden_inferred_bound = opaque type `{$ty}` does not satisfy its ass
.specifically = this associated type bound is unsatisfied for `{$proj_ty}`

lint_opaque_hidden_inferred_bound_sugg = add this bound

lint_useless_anonymous_reexport = useless anonymous re-export
.note = only anonymous re-exports of traits are useful, this is {$article} `{$desc}`
3 changes: 0 additions & 3 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ mod opaque_hidden_inferred_bound;
mod pass_by_value;
mod passes;
mod redundant_semicolon;
mod reexports;
mod traits;
mod types;
mod unused;
Expand Down Expand Up @@ -112,7 +111,6 @@ use noop_method_call::*;
use opaque_hidden_inferred_bound::*;
use pass_by_value::*;
use redundant_semicolon::*;
use reexports::*;
use traits::*;
use types::*;
use unused::*;
Expand Down Expand Up @@ -244,7 +242,6 @@ late_lint_methods!(
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
MapUnitFn: MapUnitFn,
UselessAnonymousReexport: UselessAnonymousReexport,
]
]
);
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,11 +1528,3 @@ pub struct UnusedAllocationDiag;
#[derive(LintDiagnostic)]
#[diag(lint_unused_allocation_mut)]
pub struct UnusedAllocationMutDiag;

#[derive(LintDiagnostic)]
#[diag(lint_useless_anonymous_reexport)]
#[note]
pub struct UselessAnonymousReexportDiag {
pub article: &'static str,
pub desc: &'static str,
}
82 changes: 0 additions & 82 deletions compiler/rustc_lint/src/reexports.rs

This file was deleted.

42 changes: 40 additions & 2 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use rustc_ast::visit::{self, Visitor};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{pluralize, MultiSpan};
use rustc_hir::def::{DefKind, Res};
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::symbol::Ident;
use rustc_span::symbol::{kw, Ident};
use rustc_span::{Span, DUMMY_SP};

struct UnusedImport<'a> {
Expand All @@ -58,6 +59,7 @@ struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
base_use_tree: Option<&'a ast::UseTree>,
base_id: ast::NodeId,
item_span: Span,
base_use_is_pub: bool,
}

struct ExternCrateToLint {
Expand Down Expand Up @@ -110,6 +112,35 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
unused: Default::default(),
})
}

fn check_import_as_underscore(&mut self, item: &ast::UseTree, id: ast::NodeId) {
match item.kind {
ast::UseTreeKind::Simple(Some(ident)) => {
if ident.name == kw::Underscore
&& !self
.r
.import_res_map
.get(&id)
.map(|per_ns| {
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
})
})
.unwrap_or(false)
{
self.unused_import(self.base_id).add(id);
}
}
ast::UseTreeKind::Nested(ref items) => self.check_imports_as_underscore(items),
_ => {}
}
}

fn check_imports_as_underscore(&mut self, items: &[(ast::UseTree, ast::NodeId)]) {
for (item, id) in items {
self.check_import_as_underscore(item, *id);
}
}
}

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

if self.base_use_is_pub {
self.check_import_as_underscore(use_tree, id);
return;
}

if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
if items.is_empty() {
self.unused_import(self.base_id).add(id);
Expand Down Expand Up @@ -300,6 +337,7 @@ impl Resolver<'_, '_> {
base_use_tree: None,
base_id: ast::DUMMY_NODE_ID,
item_span: DUMMY_SP,
base_use_is_pub: false,
};
visit::walk_crate(&mut visitor, krate);

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-99695-b.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
#![allow(unused, nonstandard_style)]
mod m {

mod p {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-99695-b.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
#![allow(unused, nonstandard_style)]
mod m {

mod p {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-99695.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
#![allow(unused, nonstandard_style)]
mod m {
#[macro_export]
macro_rules! nu {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-99695.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
#![allow(unused, nonstandard_style)]
mod m {
#[macro_export]
macro_rules! nu {
Expand Down
16 changes: 7 additions & 9 deletions tests/ui/lint/anonymous-reexport.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(useless_anonymous_reexport)]
#![deny(unused_imports)]
#![crate_type = "rlib"]

mod my_mod {
Expand All @@ -9,13 +9,11 @@ mod my_mod {
}

pub use self::my_mod::Foo as _;
pub use self::my_mod::TyFoo as _;
pub use self::my_mod::Bar as _; //~ ERROR
pub use self::my_mod::TyBar as _; //~ ERROR
pub use self::my_mod::{Bar as _}; //~ ERROR
pub use self::my_mod::{Bar as _, Foo as _}; //~ ERROR
pub use self::my_mod::{Bar as _, TyBar as _};
//~^ ERROR
//~| ERROR
pub use self::my_mod::TyFoo as _; //~ ERROR unused import
pub use self::my_mod::Bar as _; //~ ERROR unused import
pub use self::my_mod::TyBar as _; //~ ERROR unused import
pub use self::my_mod::{Bar as _}; //~ ERROR unused import
pub use self::my_mod::{Bar as _, Foo as _}; //~ ERROR unused import
pub use self::my_mod::{Bar as _, TyBar as _}; //~ ERROR unused imports
#[allow(unused_imports)]
use self::my_mod::TyBar as _;
49 changes: 19 additions & 30 deletions tests/ui/lint/anonymous-reexport.stderr
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
error: useless anonymous re-export
--> $DIR/anonymous-reexport.rs:13:1
error: unused import: `self::my_mod::TyFoo as _`
--> $DIR/anonymous-reexport.rs:12:9
|
LL | pub use self::my_mod::Bar as _;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | pub use self::my_mod::TyFoo as _;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: only anonymous re-exports of traits are useful, this is a `struct`
note: the lint level is defined here
--> $DIR/anonymous-reexport.rs:1:9
|
LL | #![deny(useless_anonymous_reexport)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: useless anonymous re-export
--> $DIR/anonymous-reexport.rs:14:1
error: unused import: `self::my_mod::Bar as _`
--> $DIR/anonymous-reexport.rs:13:9
|
LL | pub use self::my_mod::TyBar as _;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | pub use self::my_mod::Bar as _;
| ^^^^^^^^^^^^^^^^^^^^^^

error: unused import: `self::my_mod::TyBar as _`
--> $DIR/anonymous-reexport.rs:14:9
|
= note: only anonymous re-exports of traits are useful, this is a `type alias`
LL | pub use self::my_mod::TyBar as _;
| ^^^^^^^^^^^^^^^^^^^^^^^^

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

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

error: useless anonymous re-export
error: unused imports: `Bar as _`, `TyBar as _`
--> $DIR/anonymous-reexport.rs:17:24
|
LL | pub use self::my_mod::{Bar as _, TyBar as _};
| ^^^^^^^^
|
= note: only anonymous re-exports of traits are useful, this is a `struct`

error: useless anonymous re-export
--> $DIR/anonymous-reexport.rs:17:34
|
LL | pub use self::my_mod::{Bar as _, TyBar as _};
| ^^^^^^^^^^
|
= note: only anonymous re-exports of traits are useful, this is a `type alias`
| ^^^^^^^^ ^^^^^^^^^^

error: aborting due to 6 previous errors