mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-13 07:24:00 +00:00
Move useless_anynous_reexport lint into unused_imports
This commit is contained in:
parent
ef03fda339
commit
825f0888cc
@ -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}`
|
.specifically = this associated type bound is unsatisfied for `{$proj_ty}`
|
||||||
|
|
||||||
lint_opaque_hidden_inferred_bound_sugg = add this bound
|
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}`
|
|
||||||
|
@ -74,7 +74,6 @@ mod opaque_hidden_inferred_bound;
|
|||||||
mod pass_by_value;
|
mod pass_by_value;
|
||||||
mod passes;
|
mod passes;
|
||||||
mod redundant_semicolon;
|
mod redundant_semicolon;
|
||||||
mod reexports;
|
|
||||||
mod traits;
|
mod traits;
|
||||||
mod types;
|
mod types;
|
||||||
mod unused;
|
mod unused;
|
||||||
@ -112,7 +111,6 @@ use noop_method_call::*;
|
|||||||
use opaque_hidden_inferred_bound::*;
|
use opaque_hidden_inferred_bound::*;
|
||||||
use pass_by_value::*;
|
use pass_by_value::*;
|
||||||
use redundant_semicolon::*;
|
use redundant_semicolon::*;
|
||||||
use reexports::*;
|
|
||||||
use traits::*;
|
use traits::*;
|
||||||
use types::*;
|
use types::*;
|
||||||
use unused::*;
|
use unused::*;
|
||||||
@ -244,7 +242,6 @@ late_lint_methods!(
|
|||||||
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
|
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
|
||||||
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
|
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
|
||||||
MapUnitFn: MapUnitFn,
|
MapUnitFn: MapUnitFn,
|
||||||
UselessAnonymousReexport: UselessAnonymousReexport,
|
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
@ -1528,11 +1528,3 @@ pub struct UnusedAllocationDiag;
|
|||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(lint_unused_allocation_mut)]
|
#[diag(lint_unused_allocation_mut)]
|
||||||
pub struct UnusedAllocationMutDiag;
|
pub struct UnusedAllocationMutDiag;
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
|
||||||
#[diag(lint_useless_anonymous_reexport)]
|
|
||||||
#[note]
|
|
||||||
pub struct UselessAnonymousReexportDiag {
|
|
||||||
pub article: &'static str,
|
|
||||||
pub desc: &'static str,
|
|
||||||
}
|
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
use crate::lints::UselessAnonymousReexportDiag;
|
|
||||||
use crate::{LateContext, LateLintPass, LintContext};
|
|
||||||
use rustc_hir::def::DefKind;
|
|
||||||
use rustc_hir::def_id::DefId;
|
|
||||||
use rustc_hir::{Item, ItemKind, UseKind};
|
|
||||||
use rustc_middle::ty::Visibility;
|
|
||||||
use rustc_span::symbol::kw;
|
|
||||||
use rustc_span::Span;
|
|
||||||
|
|
||||||
declare_lint! {
|
|
||||||
/// The `useless_anonymous_reexport` lint checks if anonymous re-exports
|
|
||||||
/// are re-exports of traits.
|
|
||||||
///
|
|
||||||
/// ### Example
|
|
||||||
///
|
|
||||||
/// ```rust,compile_fail
|
|
||||||
/// #![deny(useless_anonymous_reexport)]
|
|
||||||
///
|
|
||||||
/// mod sub {
|
|
||||||
/// pub struct Bar;
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// pub use self::sub::Bar as _;
|
|
||||||
/// # fn main() {}
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// {{produces}}
|
|
||||||
///
|
|
||||||
/// ### Explanation
|
|
||||||
///
|
|
||||||
/// Anonymous re-exports are only useful if it's a re-export of a trait
|
|
||||||
/// in case you want to give access to it. If you re-export any other kind,
|
|
||||||
/// you won't be able to use it since its name won't be accessible.
|
|
||||||
pub USELESS_ANONYMOUS_REEXPORT,
|
|
||||||
Warn,
|
|
||||||
"useless anonymous re-export"
|
|
||||||
}
|
|
||||||
|
|
||||||
declare_lint_pass!(UselessAnonymousReexport => [USELESS_ANONYMOUS_REEXPORT]);
|
|
||||||
|
|
||||||
fn emit_err(cx: &LateContext<'_>, span: Span, def_id: DefId) {
|
|
||||||
let article = cx.tcx.def_descr_article(def_id);
|
|
||||||
let desc = cx.tcx.def_descr(def_id);
|
|
||||||
cx.emit_spanned_lint(
|
|
||||||
USELESS_ANONYMOUS_REEXPORT,
|
|
||||||
span,
|
|
||||||
UselessAnonymousReexportDiag { article, desc },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for UselessAnonymousReexport {
|
|
||||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
|
||||||
if let ItemKind::Use(path, kind) = item.kind &&
|
|
||||||
!matches!(kind, UseKind::Glob) &&
|
|
||||||
item.ident.name == kw::Underscore &&
|
|
||||||
// We only want re-exports. If it's just a `use X;`, then we ignore it.
|
|
||||||
match cx.tcx.local_visibility(item.owner_id.def_id) {
|
|
||||||
Visibility::Public => true,
|
|
||||||
Visibility::Restricted(level) => {
|
|
||||||
level != cx.tcx.parent_module_from_def_id(item.owner_id.def_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
for def_id in path.res.iter().filter_map(|r| r.opt_def_id()) {
|
|
||||||
match cx.tcx.def_kind(def_id) {
|
|
||||||
DefKind::Trait | DefKind::TraitAlias => {}
|
|
||||||
DefKind::TyAlias => {
|
|
||||||
let ty = cx.tcx.type_of(def_id);
|
|
||||||
if !ty.0.is_trait() {
|
|
||||||
emit_err(cx, item.span, def_id);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
emit_err(cx, item.span, def_id);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -32,9 +32,10 @@ use rustc_ast::visit::{self, Visitor};
|
|||||||
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
||||||
use rustc_data_structures::unord::UnordSet;
|
use rustc_data_structures::unord::UnordSet;
|
||||||
use rustc_errors::{pluralize, MultiSpan};
|
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::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS};
|
||||||
use rustc_session::lint::BuiltinLintDiagnostics;
|
use rustc_session::lint::BuiltinLintDiagnostics;
|
||||||
use rustc_span::symbol::Ident;
|
use rustc_span::symbol::{kw, Ident};
|
||||||
use rustc_span::{Span, DUMMY_SP};
|
use rustc_span::{Span, DUMMY_SP};
|
||||||
|
|
||||||
struct UnusedImport<'a> {
|
struct UnusedImport<'a> {
|
||||||
@ -58,6 +59,7 @@ struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
|
|||||||
base_use_tree: Option<&'a ast::UseTree>,
|
base_use_tree: Option<&'a ast::UseTree>,
|
||||||
base_id: ast::NodeId,
|
base_id: ast::NodeId,
|
||||||
item_span: Span,
|
item_span: Span,
|
||||||
|
base_use_is_pub: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ExternCrateToLint {
|
struct ExternCrateToLint {
|
||||||
@ -110,6 +112,35 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
|
|||||||
unused: Default::default(),
|
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> {
|
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> {
|
|||||||
// whether they're used or not. Also ignore imports with a dummy span
|
// 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
|
// because this means that they were generated in some fashion by the
|
||||||
// compiler and we don't need to consider them.
|
// 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) => {
|
ast::ItemKind::ExternCrate(orig_name) => {
|
||||||
self.extern_crate_items.push(ExternCrateToLint {
|
self.extern_crate_items.push(ExternCrateToLint {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
@ -146,6 +178,11 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
|
|||||||
self.base_use_tree = Some(use_tree);
|
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 let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
|
||||||
if items.is_empty() {
|
if items.is_empty() {
|
||||||
self.unused_import(self.base_id).add(id);
|
self.unused_import(self.base_id).add(id);
|
||||||
@ -300,6 +337,7 @@ impl Resolver<'_, '_> {
|
|||||||
base_use_tree: None,
|
base_use_tree: None,
|
||||||
base_id: ast::DUMMY_NODE_ID,
|
base_id: ast::DUMMY_NODE_ID,
|
||||||
item_span: DUMMY_SP,
|
item_span: DUMMY_SP,
|
||||||
|
base_use_is_pub: false,
|
||||||
};
|
};
|
||||||
visit::walk_crate(&mut visitor, krate);
|
visit::walk_crate(&mut visitor, krate);
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
|
#![allow(unused, nonstandard_style)]
|
||||||
mod m {
|
mod m {
|
||||||
|
|
||||||
mod p {
|
mod p {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
|
#![allow(unused, nonstandard_style)]
|
||||||
mod m {
|
mod m {
|
||||||
|
|
||||||
mod p {
|
mod p {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
|
#![allow(unused, nonstandard_style)]
|
||||||
mod m {
|
mod m {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! nu {
|
macro_rules! nu {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![allow(unused, nonstandard_style, useless_anonymous_reexport)]
|
#![allow(unused, nonstandard_style)]
|
||||||
mod m {
|
mod m {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! nu {
|
macro_rules! nu {
|
||||||
|
Loading…
Reference in New Issue
Block a user