Auto merge of #42588 - ishitatsuyuki:patch-1, r=petrochenkov

Make unused-extern-crate warn-by-default

Apart from enabling the lint, this pull request also removes existing unused crates in the codebase, and fix some amount of false positives on crates with special purposes.

Now that all false positive issues are closed, it should be possible to make it available to wider users.

Quote:
> Now that macro modularization is implemented, this is true today! *https://github.com/rust-lang/rust/issues/30849#issuecomment-286573218*

Concerns: can break some `#[deny(warnings)]`.

Close #42591
This commit is contained in:
bors 2017-08-27 10:02:51 +00:00
commit 78e95bb7ac
51 changed files with 96 additions and 38 deletions

4
src/Cargo.lock generated
View file

@ -1327,7 +1327,6 @@ dependencies = [
"graphviz 0.0.0",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0",
"rustc_data_structures 0.0.0",
"rustc_errors 0.0.0",
"rustc_mir 0.0.0",
"syntax 0.0.0",
@ -1464,7 +1463,6 @@ dependencies = [
"proc_macro 0.0.0",
"rustc 0.0.0",
"rustc_back 0.0.0",
"rustc_const_math 0.0.0",
"rustc_data_structures 0.0.0",
"rustc_errors 0.0.0",
"serialize 0.0.0",
@ -1605,6 +1603,7 @@ dependencies = [
name = "rustc_tsan"
version = "0.0.0"
dependencies = [
"alloc 0.0.0",
"alloc_system 0.0.0",
"build_helper 0.1.0",
"cmake 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1859,7 +1858,6 @@ name = "syntax_ext"
version = "0.0.0"
dependencies = [
"fmt_macros 0.0.0",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"proc_macro 0.0.0",
"rustc_errors 0.0.0",
"syntax 0.0.0",

View file

@ -10,7 +10,6 @@
#![deny(warnings)]
#![feature(alloc)]
#![feature(attr_literals)]
#![feature(box_syntax)]
#![feature(inclusive_range_syntax)]
@ -27,14 +26,10 @@
#![feature(splice)]
#![feature(str_escape)]
#![feature(string_retain)]
#![feature(test)]
#![feature(unboxed_closures)]
#![feature(unicode)]
extern crate alloc;
extern crate test;
extern crate std_unicode;
extern crate core;
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;

View file

@ -19,7 +19,7 @@
#![feature(libc)]
#![feature(linkage)]
#![feature(staged_api)]
#![cfg_attr(dummy_jemalloc, allow(dead_code))]
#![cfg_attr(dummy_jemalloc, allow(dead_code, unused_extern_crates))]
#![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
extern crate alloc;

View file

@ -26,7 +26,6 @@
#![feature(inclusive_range)]
#![feature(inclusive_range_syntax)]
#![feature(iter_rfind)]
#![feature(libc)]
#![feature(nonzero)]
#![feature(ord_max_min)]
#![feature(rand)]
@ -41,13 +40,10 @@
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_from)]
#![feature(unicode)]
#![feature(unique)]
extern crate core;
extern crate test;
extern crate libc;
extern crate std_unicode;
extern crate rand;
mod any;

View file

@ -34,7 +34,9 @@
#![feature(core_intrinsics)]
#![feature(lang_items)]
#![feature(libc)]
#![feature(panic_unwind)]
#![cfg_attr(not(any(target_env = "msvc",
all(windows, target_arch = "x86_64", target_env = "gnu"))),
feature(panic_unwind))]
#![feature(raw)]
#![feature(staged_api)]
#![feature(unwind_attributes)]
@ -45,6 +47,7 @@
extern crate alloc;
extern crate libc;
#[cfg(not(any(target_env = "msvc", all(windows, target_arch = "x86_64", target_env = "gnu"))))]
extern crate unwind;
use core::intrinsics;

View file

@ -522,6 +522,8 @@ define_dep_nodes!( <'tcx>
[] DylibDepFormats(DefId),
[] IsAllocator(DefId),
[] IsPanicRuntime(DefId),
[] IsCompilerBuiltins(DefId),
[] HasGlobalAllocator(DefId),
[] ExternCrate(DefId),
[] LintLevels,
);

View file

@ -26,7 +26,7 @@
#![feature(core_intrinsics)]
#![feature(discriminant_value)]
#![feature(i128_type)]
#![feature(libc)]
#![cfg_attr(windows, feature(libc))]
#![feature(never_type)]
#![feature(nonzero)]
#![feature(quote)]
@ -45,6 +45,7 @@ extern crate core;
extern crate fmt_macros;
extern crate getopts;
extern crate graphviz;
#[cfg(windows)]
extern crate libc;
extern crate owning_ref;
extern crate rustc_back;
@ -62,7 +63,9 @@ extern crate serialize as rustc_serialize; // used by deriving
// Note that librustc doesn't actually depend on these crates, see the note in
// `Cargo.toml` for this crate about why these are here.
#[allow(unused_extern_crates)]
extern crate flate2;
#[allow(unused_extern_crates)]
extern crate test;
#[macro_use]

View file

@ -30,7 +30,7 @@ declare_lint! {
declare_lint! {
pub UNUSED_EXTERN_CRATES,
Allow,
Warn,
"extern crates that are never used"
}

View file

@ -797,6 +797,8 @@ pub struct GlobalCtxt<'tcx> {
pub maybe_unused_trait_imports: NodeSet,
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
// Internal cache for metadata decoding. No need to track deps on this.
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@ -1038,6 +1040,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
mir_passes,
freevars: RefCell::new(resolutions.freevars),
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates,
rcache: RefCell::new(FxHashMap()),
normalized_cache: RefCell::new(FxHashMap()),
inhabitedness_cache: RefCell::new(FxHashMap()),

View file

@ -516,6 +516,18 @@ impl<'tcx> QueryDescription for queries::is_panic_runtime<'tcx> {
}
}
impl<'tcx> QueryDescription for queries::is_compiler_builtins<'tcx> {
fn describe(_: TyCtxt, _: DefId) -> String {
"checking if the crate is_compiler_builtins".to_string()
}
}
impl<'tcx> QueryDescription for queries::has_global_allocator<'tcx> {
fn describe(_: TyCtxt, _: DefId) -> String {
"checking if the crate has_global_allocator".to_string()
}
}
impl<'tcx> QueryDescription for queries::extern_crate<'tcx> {
fn describe(_: TyCtxt, _: DefId) -> String {
"getting crate's ExternCrateData".to_string()
@ -1079,6 +1091,8 @@ define_maps! { <'tcx>
[] is_allocator: IsAllocator(DefId) -> bool,
[] is_panic_runtime: IsPanicRuntime(DefId) -> bool,
[] is_compiler_builtins: IsCompilerBuiltins(DefId) -> bool,
[] has_global_allocator: HasGlobalAllocator(DefId) -> bool,
[] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,

View file

@ -131,6 +131,7 @@ pub struct Resolutions {
pub freevars: FreevarMap,
pub trait_map: TraitMap,
pub maybe_unused_trait_imports: NodeSet,
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
pub export_map: ExportMap,
}

View file

@ -15,6 +15,5 @@ syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
graphviz = { path = "../libgraphviz" }
rustc = { path = "../librustc" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_mir = { path = "../librustc_mir" }
rustc_errors = { path = "../librustc_errors" }

View file

@ -28,9 +28,7 @@ extern crate rustc_errors as errors;
extern crate graphviz as dot;
#[macro_use]
extern crate rustc;
extern crate rustc_data_structures;
extern crate rustc_mir;
extern crate core; // for NonZero
pub use borrowck::check_crate;
pub use borrowck::build_borrowck_dataflow_data_for_fn;

View file

@ -877,6 +877,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
export_map: resolver.export_map,
trait_map: resolver.trait_map,
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates,
},
hir_forest,
})

View file

@ -20,7 +20,7 @@
#![deny(warnings)]
#![feature(box_syntax)]
#![feature(libc)]
#![cfg_attr(unix, feature(libc))]
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
#![feature(set_stdio)]
@ -29,6 +29,7 @@ extern crate arena;
extern crate getopts;
extern crate graphviz;
extern crate env_logger;
#[cfg(unix)]
extern crate libc;
extern crate rustc;
extern crate rustc_allocator;

View file

@ -16,10 +16,11 @@
#![feature(custom_attribute)]
#![allow(unused_attributes)]
#![feature(range_contains)]
#![feature(libc)]
#![cfg_attr(unix, feature(libc))]
#![feature(conservative_impl_trait)]
extern crate term;
#[cfg(unix)]
extern crate libc;
extern crate serialize as rustc_serialize;
extern crate syntax_pos;

View file

@ -15,7 +15,6 @@ owning_ref = "0.3.3"
proc_macro = { path = "../libproc_macro" }
rustc = { path = "../librustc" }
rustc_back = { path = "../librustc_back" }
rustc_const_math = { path = "../librustc_const_math" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_errors = { path = "../librustc_errors" }
serialize = { path = "../libserialize" }

View file

@ -137,6 +137,8 @@ provide! { <'tcx> tcx, def_id, cdata,
dylib_dependency_formats => { Rc::new(cdata.get_dylib_dependency_formats(&tcx.dep_graph)) }
is_panic_runtime => { cdata.is_panic_runtime(&tcx.dep_graph) }
is_compiler_builtins => { cdata.is_compiler_builtins(&tcx.dep_graph) }
has_global_allocator => { cdata.has_global_allocator(&tcx.dep_graph) }
extern_crate => { Rc::new(cdata.extern_crate.get()) }
}

View file

@ -39,7 +39,6 @@ extern crate proc_macro;
#[macro_use]
extern crate rustc;
extern crate rustc_back;
extern crate rustc_const_math;
extern crate rustc_data_structures;
mod diagnostics;

View file

@ -121,9 +121,7 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
directive.vis.get() == ty::Visibility::Public ||
directive.span.source_equal(&DUMMY_SP) => {}
ImportDirectiveSubclass::ExternCrate => {
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
let msg = "unused extern crate";
; resolver.session.buffer_lint(lint, directive.id, directive.span, msg)
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
}
ImportDirectiveSubclass::MacroUse => {
let lint = lint::builtin::UNUSED_IMPORTS;

View file

@ -1250,6 +1250,7 @@ pub struct Resolver<'a> {
used_imports: FxHashSet<(NodeId, Namespace)>,
pub maybe_unused_trait_imports: NodeSet,
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
/// privacy errors are delayed until the end in order to deduplicate them
privacy_errors: Vec<PrivacyError<'a>>,
@ -1457,6 +1458,7 @@ impl<'a> Resolver<'a> {
used_imports: FxHashSet(),
maybe_unused_trait_imports: NodeSet(),
maybe_unused_extern_crates: Vec::new(),
privacy_errors: Vec::new(),
ambiguity_errors: Vec::new(),

View file

@ -14,5 +14,6 @@ build_helper = { path = "../build_helper" }
cmake = "0.1.18"
[dependencies]
alloc = { path = "../liballoc" }
alloc_system = { path = "../liballoc_system" }
core = { path = "../libcore" }

View file

@ -9,8 +9,10 @@
// except according to those terms.
#![sanitizer_runtime]
#![feature(sanitizer_runtime)]
#![feature(alloc_system)]
#![feature(allocator_api)]
#![feature(global_allocator)]
#![feature(sanitizer_runtime)]
#![feature(staged_api)]
#![no_std]
#![unstable(feature = "sanitizer_runtime_lib",
@ -18,3 +20,8 @@
issue = "0")]
extern crate alloc_system;
use alloc_system::System;
#[global_allocator]
static ALLOC: System = System;

View file

@ -72,4 +72,15 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let mut visitor = CheckVisitor { tcx, used_trait_imports };
tcx.hir.krate().visit_all_item_likes(&mut visitor);
for &(id, span) in &tcx.maybe_unused_extern_crates {
let cnum = tcx.sess.cstore.extern_mod_stmt_cnum(id).unwrap().as_def_id();
if !tcx.is_compiler_builtins(cnum)
&& !tcx.is_panic_runtime(cnum)
&& !tcx.has_global_allocator(cnum) {
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
let msg = "unused extern crate";
tcx.lint_node(lint, id, span, msg);
}
}
}

View file

@ -358,6 +358,7 @@ extern crate std_unicode;
extern crate libc;
// We always need an unwinder currently for backtraces
#[allow(unused_extern_crates)]
extern crate unwind;
// compiler-rt intrinsics

View file

@ -23,6 +23,7 @@
//! On a technical level, Rust inserts
//!
//! ```
//! # #[allow(unused_extern_crates)]
//! extern crate std;
//! ```
//!

View file

@ -154,8 +154,6 @@ pub trait OpenOptionsExt {
/// # Examples
///
/// ```no_run
/// # #![feature(libc)]
/// extern crate libc;
/// use std::fs::OpenOptions;
/// use std::os::unix::fs::OpenOptionsExt;
///

View file

@ -10,7 +10,6 @@ crate-type = ["dylib"]
[dependencies]
fmt_macros = { path = "../libfmt_macros" }
log = "0.3"
proc_macro = { path = "../libproc_macro" }
rustc_errors = { path = "../librustc_errors" }
syntax = { path = "../libsyntax" }

View file

@ -18,7 +18,6 @@
#![feature(proc_macro_internals)]
extern crate fmt_macros;
extern crate log;
#[macro_use]
extern crate syntax;
extern crate syntax_pos;

View file

@ -35,13 +35,14 @@
#![deny(warnings)]
#![feature(asm)]
#![feature(libc)]
#![cfg_attr(unix, feature(libc))]
#![feature(set_stdio)]
#![feature(panic_unwind)]
#![feature(staged_api)]
extern crate getopts;
extern crate term;
#[cfg(unix)]
extern crate libc;
extern crate panic_unwind;

View file

@ -15,6 +15,7 @@
// libsyntax is not compiled for it.
#![deny(plugin_as_library)]
#![allow(unused_extern_crates)]
extern crate macro_crate_test; //~ ERROR compiler plugin used as an ordinary library

View file

@ -9,6 +9,7 @@
// except according to those terms.
#![feature(alloc)]
#![allow(unused_extern_crates)]
extern crate alloc;
//~^ NOTE previous import of the extern crate `alloc` here

View file

@ -9,6 +9,7 @@
// except according to those terms.
#![feature(alloc, libc)]
#![allow(unused_extern_crates)]
extern crate alloc;
//~^ NOTE previous import of the extern crate `alloc` here

View file

@ -9,6 +9,7 @@
// except according to those terms.
#![feature(alloc)]
#![allow(unused_extern_crates)]
extern crate alloc;
//~^ NOTE previous import of the extern crate `alloc` here

View file

@ -0,0 +1,9 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

View file

@ -16,6 +16,7 @@
#![deny(non_snake_case)] // To trigger a hard error
// Shouldn't generate a warning about unstable features
#[allow(unused_extern_crates)]
extern crate stability_cfg2;
pub fn BOGUS() { } //~ ERROR

View file

@ -11,6 +11,7 @@
// aux-build:issue-36881-aux.rs
fn main() {
#[allow(unused_extern_crates)]
extern crate issue_36881_aux;
use issue_36881_aux::Foo; //~ ERROR unresolved import
}

View file

@ -14,7 +14,7 @@
// aux-build:stability_cfg2.rs
#![warn(deprecated)]
#![allow(dead_code)]
#![allow(dead_code, unused_extern_crates)]
#![feature(staged_api, test_feature, rustc_attrs)]
#![stable(feature = "rust1", since = "1.0.0")]

View file

@ -12,12 +12,15 @@
// aux-build:lint_unused_extern_crate2.rs
// aux-build:lint_unused_extern_crate3.rs
// aux-build:lint_unused_extern_crate4.rs
// aux-build:lint_unused_extern_crate5.rs
#![deny(unused_extern_crates)]
#![allow(unused_variables)]
#![allow(deprecated)]
extern crate lint_unused_extern_crate4; //~ ERROR: unused extern crate
extern crate lint_unused_extern_crate5; //~ ERROR: unused extern crate
pub extern crate lint_unused_extern_crate4; // no error, it is reexported
extern crate lint_unused_extern_crate3; // no error, it is used

View file

@ -11,5 +11,6 @@
#![no_std]
#![feature(macro_reexport)]
#[allow(unused_extern_crates)]
#[macro_reexport] //~ ERROR bad macro reexport
extern crate std;

View file

@ -11,5 +11,6 @@
#![no_std]
#![feature(macro_reexport)]
#[allow(unused_extern_crates)]
#[macro_reexport="foo"] //~ ERROR bad macro reexport
extern crate std;

View file

@ -11,5 +11,6 @@
#![no_std]
#![feature(macro_reexport)]
#[allow(unused_extern_crates)]
#[macro_reexport(foo="bar")] //~ ERROR bad macro reexport
extern crate std;

View file

@ -10,5 +10,6 @@
#![no_std]
#[allow(unused_extern_crates)]
#[macro_use(foo(bar))] //~ ERROR bad macro import
extern crate std;

View file

@ -10,5 +10,6 @@
#![no_std]
#[allow(unused_extern_crates)]
#[macro_use(foo="bar")] //~ ERROR bad macro import
extern crate std;

View file

@ -9,6 +9,7 @@
// except according to those terms.
#![no_std]
#![allow(unused_extern_crates)]
extern crate core; //~ ERROR: the name `core` is defined multiple times
extern crate std;

View file

@ -12,8 +12,6 @@
#![feature(placement_in_syntax)]
extern crate core;
fn main() {
use std::boxed::HEAP; //~ ERROR use of unstable library feature

View file

@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[allow(unused_extern_crates)]
extern crate std;
//~^ ERROR the name `std` is defined multiple times

View file

@ -9,6 +9,7 @@
// except according to those terms.
#![feature(alloc)]
#![allow(unused_extern_crates)]
mod a {
extern crate alloc;

View file

@ -9,7 +9,7 @@
// except according to those terms.
#![deny(unused_attributes)]
#![allow(dead_code, unused_imports)]
#![allow(dead_code, unused_imports, unused_extern_crates)]
#![feature(custom_attribute)]
#![foo] //~ ERROR unused attribute

@ -1 +1 @@
Subproject commit 7847f75bb6fc4fee6f511ca740bbe6872c08502b
Subproject commit 3d3f2c05d742e5f907e951aa8849b03f0bc1a895

View file

@ -14,6 +14,7 @@
#![deny(warnings)]
#[cfg(any(target_os = "macos", target_os = "ios"))]
extern crate libc;
extern crate test;
extern crate getopts;