use visibility to check unused imports and delete some stmts

This commit is contained in:
bohan 2023-10-15 19:38:22 +08:00
parent 724ba7fe90
commit 482275b194
38 changed files with 133 additions and 55 deletions

View File

@ -53,7 +53,6 @@ pub mod visit;
pub use self::ast::*;
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasSpan, HasTokens};
pub use self::format::*;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};

View File

@ -50,7 +50,6 @@ mod utils;
pub use self::create_scope_map::compute_mir_scopes;
pub use self::metadata::build_global_var_di_node;
pub use self::metadata::extend_scope_to_file;
#[allow(non_upper_case_globals)]
const DW_TAG_auto_variable: c_uint = 0x100;

View File

@ -6,7 +6,6 @@ use crate::llvm;
use crate::type_of::LayoutLlvmExt;
use rustc_codegen_ssa::traits::*;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
pub use rustc_middle::mir::mono::MonoItem;
use rustc_middle::mir::mono::{Linkage, Visibility};
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
use rustc_middle::ty::{self, Instance, TypeVisitableExt};

View File

@ -4,9 +4,7 @@ mod arg_matrix;
mod checks;
mod suggestions;
pub use _impl::*;
use rustc_errors::ErrorGuaranteed;
pub use suggestions::*;
use crate::coercion::DynamicCoerceMany;
use crate::{Diverges, EnclosingBreakables, Inherited};

View File

@ -19,7 +19,6 @@ use rustc_span::Span;
pub use self::FulfillmentErrorCode::*;
pub use self::ImplSource::*;
pub use self::ObligationCauseCode::*;
pub use self::SelectionError::*;
pub use self::engine::{TraitEngine, TraitEngineExt};

View File

@ -3,12 +3,10 @@ use rustc_hir::LangItem;
use smallvec::SmallVec;
use super::{BasicBlock, InlineAsmOperand, Operand, SourceInfo, TerminatorKind, UnwindAction};
pub use rustc_ast::Mutability;
use rustc_macros::HashStable;
use std::iter;
use std::slice;
pub use super::query::*;
use super::*;
impl SwitchTargets {

View File

@ -1,5 +1,3 @@
pub use self::AssocItemContainer::*;
use crate::ty;
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
use rustc_hir as hir;

View File

@ -48,7 +48,7 @@ mod visitor;
pub use self::cursor::{AnalysisResults, ResultsClonedCursor, ResultsCursor, ResultsRefCursor};
pub use self::direction::{Backward, Direction, Forward};
pub use self::engine::{Engine, EntrySets, Results, ResultsCloned};
pub use self::lattice::{JoinSemiLattice, MaybeReachable, MeetSemiLattice};
pub use self::lattice::{JoinSemiLattice, MaybeReachable};
pub use self::visitor::{visit_results, ResultsVisitable, ResultsVisitor};
/// Analysis domains are all bitsets of various kinds. This trait holds

View File

@ -59,7 +59,6 @@ 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 {
@ -146,7 +145,6 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
// 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.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,
@ -173,7 +171,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.r.effective_visibilities.is_exported(self.r.local_def_id(id)) {
self.check_import_as_underscore(use_tree, id);
return;
}
@ -332,7 +330,6 @@ 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);

View File

@ -20,7 +20,6 @@ use std::ops::ControlFlow;
pub use self::infer_ctxt_ext::*;
pub use self::type_err_ctxt_ext::*;
pub use rustc_infer::traits::error_reporting::*;
// When outputting impl candidates, prefer showing those that are more similar.
//

View File

@ -41,11 +41,6 @@ use std::ops::ControlFlow;
pub(crate) use self::project::{needs_normalization, BoundVarReplacer, PlaceholderReplacer};
pub use self::FulfillmentErrorCode::*;
pub use self::ImplSource::*;
pub use self::ObligationCauseCode::*;
pub use self::SelectionError::*;
pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
pub use self::coherence::{OrphanCheckErr, OverlapResult};
pub use self::engine::{ObligationCtxt, TraitEngineExt};

View File

@ -9,7 +9,7 @@ use rustc_middle::ty::{self, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitable
use rustc_span::Span;
use smallvec::SmallVec;
pub use rustc_infer::traits::{self, util::*};
pub use rustc_infer::traits::util::*;
///////////////////////////////////////////////////////////////////////////
// `TraitAliasExpander` iterator

View File

@ -1,5 +1,6 @@
#![doc = include_str!("../../stdarch/crates/core_arch/src/core_arch_docs.md")]
#[allow(unused_imports)]
#[stable(feature = "simd_arch", since = "1.27.0")]
pub use crate::core_arch::arch::*;

View File

@ -120,8 +120,6 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![deny(fuzzy_provenance_casts)]
extern crate test;
mod alloc;
mod any;
mod array;

View File

@ -8,8 +8,6 @@ use core::num::flt2dec::{
};
use core::num::fmt::{Formatted, Part};
pub use test::Bencher;
mod estimator;
mod strategy {
mod dragon;

View File

@ -35,6 +35,5 @@ pub mod simd {
pub use crate::core_simd::masks::*;
pub use crate::core_simd::ord::*;
pub use crate::core_simd::swizzle::*;
pub use crate::core_simd::swizzle_dyn::*;
pub use crate::core_simd::vector::*;
}

View File

@ -12,6 +12,7 @@
pub mod alloc;
pub mod small_c_string;
#[allow(unused_imports)]
pub mod thread_local;
#[cfg(test)]

View File

@ -241,6 +241,7 @@ pub unsafe fn cleanup() {
#[cfg(target_os = "android")]
pub use crate::sys::android::signal;
#[allow(unused_imports)]
#[cfg(not(target_os = "android"))]
pub use libc::signal;

View File

@ -1,7 +1,6 @@
pub use self::process_common::{Command, CommandArgs, ExitCode, Stdio, StdioPipes};
pub use self::process_inner::{ExitStatus, ExitStatusError, Process};
pub use crate::ffi::OsString as EnvKey;
pub use crate::sys_common::process::CommandEnvs;
#[cfg_attr(any(target_os = "espidf", target_os = "horizon"), allow(unused))]
mod process_common;

View File

@ -75,6 +75,7 @@ cfg_if::cfg_if! {
return 0;
}
} else {
#[allow(unused_imports)]
pub use libc::{sigemptyset, sigaddset};
}
}

View File

@ -19,6 +19,7 @@ mod in_fn_test {
}
mod blurg {
#[allow(unused_imports)]
pub use std::cmp::Ordering::*; // ok, re-export
}

View File

@ -19,6 +19,7 @@ mod in_fn_test {
}
mod blurg {
#[allow(unused_imports)]
pub use std::cmp::Ordering::*; // ok, re-export
}

View File

@ -1,4 +1,5 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#[macro_use]
mod macros;

View File

@ -4,7 +4,7 @@
mod generated;
#[allow(unreachable_pub)]
pub use self::generated::{SyntaxKind, T};
pub use self::generated::SyntaxKind;
impl From<u16> for SyntaxKind {
#[inline]

File diff suppressed because one or more lines are too long

View File

@ -450,7 +450,6 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> String {
[ident] => { $crate::SyntaxKind::IDENT };
[shebang] => { $crate::SyntaxKind::SHEBANG };
}
pub use T;
};
sourcegen::add_preamble("sourcegen_ast", sourcegen::reformat(ast.to_string()))

View File

@ -1,3 +1,5 @@
#![allow(unused_imports)]
use std::collections::{hash_set, HashSet};
use std::fmt;
use std::path::{Path, PathBuf};

View File

@ -0,0 +1,25 @@
#![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
mod d {
const D: i32 = 1;
}
pub use d::*;
//~^ ERROR: unused import: `d::*`
fn main() {}

View File

@ -0,0 +1,20 @@
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: unused import: `d::*`
--> $DIR/pub-reexport-empty.rs:22:9
|
LL | pub use d::*;
| ^^^^
error: aborting due to 2 previous errors

View File

@ -5,9 +5,12 @@ mod a {
mod foo {}
mod a {
pub use super::foo; //~ ERROR cannot be re-exported
pub use super::foo;
//~^ ERROR cannot be re-exported
//~| WARNING unused import: `super::foo`
pub use super::*;
//~^ WARNING glob import doesn't reexport anything because no candidate is public enough
//~| WARNING unused import: `super::*`
}
}

View File

@ -11,44 +11,44 @@ LL | pub use super::foo;
| ^^^^^^^^^^
error[E0603]: module import `foo` is private
--> $DIR/reexports.rs:33:15
--> $DIR/reexports.rs:36:15
|
LL | use b::a::foo::S;
| ^^^ private module import
|
note: the module import `foo` is defined here...
--> $DIR/reexports.rs:21:17
--> $DIR/reexports.rs:24:17
|
LL | pub use super::foo; // This is OK since the value `foo` is visible enough.
| ^^^^^^^^^^
note: ...and refers to the module `foo` which is defined here
--> $DIR/reexports.rs:16:5
--> $DIR/reexports.rs:19:5
|
LL | mod foo {
| ^^^^^^^
error[E0603]: module import `foo` is private
--> $DIR/reexports.rs:34:15
--> $DIR/reexports.rs:37:15
|
LL | use b::b::foo::S as T;
| ^^^ private module import
|
note: the module import `foo` is defined here...
--> $DIR/reexports.rs:26:17
--> $DIR/reexports.rs:29:17
|
LL | pub use super::*; // This is also OK since the value `foo` is visible enough.
| ^^^^^^^^
note: ...and refers to the module `foo` which is defined here
--> $DIR/reexports.rs:16:5
--> $DIR/reexports.rs:19:5
|
LL | mod foo {
| ^^^^^^^
warning: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/reexports.rs:9:17
warning: unused import: `super::foo`
--> $DIR/reexports.rs:8:17
|
LL | pub use super::*;
| ^^^^^^^^
LL | pub use super::foo;
| ^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/reexports.rs:1:9
@ -56,7 +56,19 @@ note: the lint level is defined here
LL | #![warn(unused_imports)]
| ^^^^^^^^^^^^^^
error: aborting due to 3 previous errors; 1 warning emitted
warning: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/reexports.rs:11:17
|
LL | pub use super::*;
| ^^^^^^^^
warning: unused import: `super::*`
--> $DIR/reexports.rs:11:17
|
LL | pub use super::*;
| ^^^^^^^^
error: aborting due to 3 previous errors; 3 warnings emitted
Some errors have detailed explanations: E0364, E0603.
For more information about an error, try `rustc --explain E0364`.

View File

@ -42,7 +42,7 @@ mod foo {
pub struct Square{pub p: Point, pub h: usize, pub w: usize}
}
mod bar {
pub mod bar {
// Don't ignore on 'pub use' because we're not sure if it's used or not
pub use std::cmp::PartialEq;
pub struct Square;

View File

@ -1,6 +1,6 @@
// run-rustfix
#![allow(unused_variables, dead_code)]
#![allow(unused_variables, dead_code, unused_imports)]
fn for_struct() {
let foo = 3; //~ ERROR expected `;`, found keyword `struct`

View File

@ -1,6 +1,6 @@
// run-rustfix
#![allow(unused_variables, dead_code)]
#![allow(unused_variables, dead_code, unused_imports)]
fn for_struct() {
let foo = 3 //~ ERROR expected `;`, found keyword `struct`

View File

@ -2,13 +2,17 @@
mod rank {
pub use self::Professor::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `self::Professor::*`
pub use self::Lieutenant::{JuniorGrade, Full};
//~^ ERROR `JuniorGrade` is private, and cannot be re-exported
//~| ERROR `Full` is private, and cannot be re-exported
//~| ERROR unused imports: `Full`, `JuniorGrade`
pub use self::PettyOfficer::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `self::PettyOfficer::*`
pub use self::Crewman::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `self::Crewman::*`
enum Professor {
Adjunct,

View File

@ -1,23 +1,23 @@
error[E0364]: `JuniorGrade` is private, and cannot be re-exported
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^^^^^^^^
|
note: consider marking `JuniorGrade` as `pub` in the imported module
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^^^^^^^^
error[E0364]: `Full` is private, and cannot be re-exported
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^
|
note: consider marking `Full` as `pub` in the imported module
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^
@ -34,18 +34,42 @@ note: the lint level is defined here
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^
error: unused import: `self::Professor::*`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
|
LL | pub use self::Professor::*;
| ^^^^^^^^^^^^^^^^^^
error: unused imports: `Full`, `JuniorGrade`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^^^^^^^^ ^^^^
error: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/issue-46209-private-enum-variant-reexport.rs:8:13
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
|
LL | pub use self::PettyOfficer::*;
| ^^^^^^^^^^^^^^^^^^^^^
error: unused import: `self::PettyOfficer::*`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
|
LL | pub use self::PettyOfficer::*;
| ^^^^^^^^^^^^^^^^^^^^^
error: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
|
LL | pub use self::Crewman::*;
| ^^^^^^^^^^^^^^^^
error: aborting due to 5 previous errors
error: unused import: `self::Crewman::*`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
|
LL | pub use self::Crewman::*;
| ^^^^^^^^^^^^^^^^
error: aborting due to 9 previous errors
For more information about this error, try `rustc --explain E0364`.

View File

@ -12,7 +12,9 @@ mod m3 {
#[deny(unused_imports)]
mod m4 {
pub use ::E::*; //~ ERROR glob import doesn't reexport anything
pub use ::E::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `::E::*`
}
enum E { V }

View File

@ -42,7 +42,13 @@ note: the lint level is defined here
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^
error: aborting due to 4 previous errors
error: unused import: `::E::*`
--> $DIR/private-variant-reexport.rs:15:13
|
LL | pub use ::E::*;
| ^^^^^^
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0364, E0365.
For more information about an error, try `rustc --explain E0364`.