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

Make unused-extern-crate warn-by-default #42588

Merged
merged 9 commits into from
Aug 27, 2017
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
4 changes: 1 addition & 3 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#![deny(warnings)]

#![feature(alloc)]
#![feature(attr_literals)]
#![feature(box_syntax)]
#![feature(inclusive_range_syntax)]
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc_jemalloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#![feature(inclusive_range)]
#![feature(inclusive_range_syntax)]
#![feature(iter_rfind)]
#![feature(libc)]
#![feature(nonzero)]
#![feature(ord_max_min)]
#![feature(rand)]
Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/libpanic_unwind/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ define_dep_nodes!( <'tcx>
[] DylibDepFormats(DefId),
[] IsAllocator(DefId),
[] IsPanicRuntime(DefId),
[] IsCompilerBuiltins(DefId),
[] HasGlobalAllocator(DefId),
[] ExternCrate(DefId),
[] LintLevels,
);
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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;
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare_lint! {

declare_lint! {
pub UNUSED_EXTERN_CRATES,
Allow,
Warn,
"extern crates that are never used"
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>,

Expand Down Expand Up @@ -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()),
Expand Down
14 changes: 14 additions & 0 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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>>,

Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 0 additions & 1 deletion src/librustc_borrowck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 0 additions & 2 deletions src/librustc_borrowck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
}

Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_tsan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
9 changes: 8 additions & 1 deletion src/librustc_tsan/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
// 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",
reason = "internal implementation detail of sanitizers",
issue = "0")]

extern crate alloc_system;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton I think you forgot this!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what is happening here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did what @alexcrichton forgot to do as a part of global_allocator PR.

use alloc_system::System;

#[global_allocator]
static ALLOC: System = System;
11 changes: 11 additions & 0 deletions src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/libstd/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! On a technical level, Rust inserts
//!
//! ```
//! # #[allow(unused_extern_crates)]
//! extern crate std;
//! ```
//!
Expand Down
2 changes: 0 additions & 2 deletions src/libstd/sys/unix/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
///
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax_ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax_ext/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#![feature(proc_macro_internals)]

extern crate fmt_macros;
extern crate log;
#[macro_use]
extern crate syntax;
extern crate syntax_pos;
Expand Down
Loading