Skip to content

Commit 78e95bb

Browse files
committed
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! *#30849 (comment) Concerns: can break some `#[deny(warnings)]`. Close #42591
2 parents 93cdf5e + a91bdf4 commit 78e95bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+96
-38
lines changed

src/Cargo.lock

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/liballoc/tests/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![deny(warnings)]
1212

13-
#![feature(alloc)]
1413
#![feature(attr_literals)]
1514
#![feature(box_syntax)]
1615
#![feature(inclusive_range_syntax)]
@@ -27,14 +26,10 @@
2726
#![feature(splice)]
2827
#![feature(str_escape)]
2928
#![feature(string_retain)]
30-
#![feature(test)]
3129
#![feature(unboxed_closures)]
3230
#![feature(unicode)]
3331

34-
extern crate alloc;
35-
extern crate test;
3632
extern crate std_unicode;
37-
extern crate core;
3833

3934
use std::hash::{Hash, Hasher};
4035
use std::collections::hash_map::DefaultHasher;

src/liballoc_jemalloc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#![feature(libc)]
2020
#![feature(linkage)]
2121
#![feature(staged_api)]
22-
#![cfg_attr(dummy_jemalloc, allow(dead_code))]
22+
#![cfg_attr(dummy_jemalloc, allow(dead_code, unused_extern_crates))]
2323
#![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
2424

2525
extern crate alloc;

src/libcore/tests/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#![feature(inclusive_range)]
2727
#![feature(inclusive_range_syntax)]
2828
#![feature(iter_rfind)]
29-
#![feature(libc)]
3029
#![feature(nonzero)]
3130
#![feature(ord_max_min)]
3231
#![feature(rand)]
@@ -41,13 +40,10 @@
4140
#![feature(test)]
4241
#![feature(trusted_len)]
4342
#![feature(try_from)]
44-
#![feature(unicode)]
4543
#![feature(unique)]
4644

4745
extern crate core;
4846
extern crate test;
49-
extern crate libc;
50-
extern crate std_unicode;
5147
extern crate rand;
5248

5349
mod any;

src/libpanic_unwind/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
#![feature(core_intrinsics)]
3535
#![feature(lang_items)]
3636
#![feature(libc)]
37-
#![feature(panic_unwind)]
37+
#![cfg_attr(not(any(target_env = "msvc",
38+
all(windows, target_arch = "x86_64", target_env = "gnu"))),
39+
feature(panic_unwind))]
3840
#![feature(raw)]
3941
#![feature(staged_api)]
4042
#![feature(unwind_attributes)]
@@ -45,6 +47,7 @@
4547

4648
extern crate alloc;
4749
extern crate libc;
50+
#[cfg(not(any(target_env = "msvc", all(windows, target_arch = "x86_64", target_env = "gnu"))))]
4851
extern crate unwind;
4952

5053
use core::intrinsics;

src/librustc/dep_graph/dep_node.rs

+2
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ define_dep_nodes!( <'tcx>
522522
[] DylibDepFormats(DefId),
523523
[] IsAllocator(DefId),
524524
[] IsPanicRuntime(DefId),
525+
[] IsCompilerBuiltins(DefId),
526+
[] HasGlobalAllocator(DefId),
525527
[] ExternCrate(DefId),
526528
[] LintLevels,
527529
);

src/librustc/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#![feature(core_intrinsics)]
2727
#![feature(discriminant_value)]
2828
#![feature(i128_type)]
29-
#![feature(libc)]
29+
#![cfg_attr(windows, feature(libc))]
3030
#![feature(never_type)]
3131
#![feature(nonzero)]
3232
#![feature(quote)]
@@ -45,6 +45,7 @@ extern crate core;
4545
extern crate fmt_macros;
4646
extern crate getopts;
4747
extern crate graphviz;
48+
#[cfg(windows)]
4849
extern crate libc;
4950
extern crate owning_ref;
5051
extern crate rustc_back;
@@ -62,7 +63,9 @@ extern crate serialize as rustc_serialize; // used by deriving
6263

6364
// Note that librustc doesn't actually depend on these crates, see the note in
6465
// `Cargo.toml` for this crate about why these are here.
66+
#[allow(unused_extern_crates)]
6567
extern crate flate2;
68+
#[allow(unused_extern_crates)]
6669
extern crate test;
6770

6871
#[macro_use]

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ declare_lint! {
3030

3131
declare_lint! {
3232
pub UNUSED_EXTERN_CRATES,
33-
Allow,
33+
Warn,
3434
"extern crates that are never used"
3535
}
3636

src/librustc/ty/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ pub struct GlobalCtxt<'tcx> {
797797

798798
pub maybe_unused_trait_imports: NodeSet,
799799

800+
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
801+
800802
// Internal cache for metadata decoding. No need to track deps on this.
801803
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
802804

@@ -1038,6 +1040,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10381040
mir_passes,
10391041
freevars: RefCell::new(resolutions.freevars),
10401042
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
1043+
maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates,
10411044
rcache: RefCell::new(FxHashMap()),
10421045
normalized_cache: RefCell::new(FxHashMap()),
10431046
inhabitedness_cache: RefCell::new(FxHashMap()),

src/librustc/ty/maps.rs

+14
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,18 @@ impl<'tcx> QueryDescription for queries::is_panic_runtime<'tcx> {
516516
}
517517
}
518518

519+
impl<'tcx> QueryDescription for queries::is_compiler_builtins<'tcx> {
520+
fn describe(_: TyCtxt, _: DefId) -> String {
521+
"checking if the crate is_compiler_builtins".to_string()
522+
}
523+
}
524+
525+
impl<'tcx> QueryDescription for queries::has_global_allocator<'tcx> {
526+
fn describe(_: TyCtxt, _: DefId) -> String {
527+
"checking if the crate has_global_allocator".to_string()
528+
}
529+
}
530+
519531
impl<'tcx> QueryDescription for queries::extern_crate<'tcx> {
520532
fn describe(_: TyCtxt, _: DefId) -> String {
521533
"getting crate's ExternCrateData".to_string()
@@ -1079,6 +1091,8 @@ define_maps! { <'tcx>
10791091

10801092
[] is_allocator: IsAllocator(DefId) -> bool,
10811093
[] is_panic_runtime: IsPanicRuntime(DefId) -> bool,
1094+
[] is_compiler_builtins: IsCompilerBuiltins(DefId) -> bool,
1095+
[] has_global_allocator: HasGlobalAllocator(DefId) -> bool,
10821096

10831097
[] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
10841098

src/librustc/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub struct Resolutions {
131131
pub freevars: FreevarMap,
132132
pub trait_map: TraitMap,
133133
pub maybe_unused_trait_imports: NodeSet,
134+
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
134135
pub export_map: ExportMap,
135136
}
136137

src/librustc_borrowck/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ syntax = { path = "../libsyntax" }
1515
syntax_pos = { path = "../libsyntax_pos" }
1616
graphviz = { path = "../libgraphviz" }
1717
rustc = { path = "../librustc" }
18-
rustc_data_structures = { path = "../librustc_data_structures" }
1918
rustc_mir = { path = "../librustc_mir" }
2019
rustc_errors = { path = "../librustc_errors" }

src/librustc_borrowck/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ extern crate rustc_errors as errors;
2828
extern crate graphviz as dot;
2929
#[macro_use]
3030
extern crate rustc;
31-
extern crate rustc_data_structures;
3231
extern crate rustc_mir;
33-
extern crate core; // for NonZero
3432

3533
pub use borrowck::check_crate;
3634
pub use borrowck::build_borrowck_dataflow_data_for_fn;

src/librustc_driver/driver.rs

+1
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
877877
export_map: resolver.export_map,
878878
trait_map: resolver.trait_map,
879879
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
880+
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates,
880881
},
881882
hir_forest,
882883
})

src/librustc_driver/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#![deny(warnings)]
2121

2222
#![feature(box_syntax)]
23-
#![feature(libc)]
23+
#![cfg_attr(unix, feature(libc))]
2424
#![feature(quote)]
2525
#![feature(rustc_diagnostic_macros)]
2626
#![feature(set_stdio)]
@@ -29,6 +29,7 @@ extern crate arena;
2929
extern crate getopts;
3030
extern crate graphviz;
3131
extern crate env_logger;
32+
#[cfg(unix)]
3233
extern crate libc;
3334
extern crate rustc;
3435
extern crate rustc_allocator;

src/librustc_errors/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
#![feature(custom_attribute)]
1717
#![allow(unused_attributes)]
1818
#![feature(range_contains)]
19-
#![feature(libc)]
19+
#![cfg_attr(unix, feature(libc))]
2020
#![feature(conservative_impl_trait)]
2121

2222
extern crate term;
23+
#[cfg(unix)]
2324
extern crate libc;
2425
extern crate serialize as rustc_serialize;
2526
extern crate syntax_pos;

src/librustc_metadata/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ owning_ref = "0.3.3"
1515
proc_macro = { path = "../libproc_macro" }
1616
rustc = { path = "../librustc" }
1717
rustc_back = { path = "../librustc_back" }
18-
rustc_const_math = { path = "../librustc_const_math" }
1918
rustc_data_structures = { path = "../librustc_data_structures" }
2019
rustc_errors = { path = "../librustc_errors" }
2120
serialize = { path = "../libserialize" }

src/librustc_metadata/cstore_impl.rs

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ provide! { <'tcx> tcx, def_id, cdata,
137137

138138
dylib_dependency_formats => { Rc::new(cdata.get_dylib_dependency_formats(&tcx.dep_graph)) }
139139
is_panic_runtime => { cdata.is_panic_runtime(&tcx.dep_graph) }
140+
is_compiler_builtins => { cdata.is_compiler_builtins(&tcx.dep_graph) }
141+
has_global_allocator => { cdata.has_global_allocator(&tcx.dep_graph) }
140142
extern_crate => { Rc::new(cdata.extern_crate.get()) }
141143
}
142144

src/librustc_metadata/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extern crate proc_macro;
3939
#[macro_use]
4040
extern crate rustc;
4141
extern crate rustc_back;
42-
extern crate rustc_const_math;
4342
extern crate rustc_data_structures;
4443

4544
mod diagnostics;

src/librustc_resolve/check_unused.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
121121
directive.vis.get() == ty::Visibility::Public ||
122122
directive.span.source_equal(&DUMMY_SP) => {}
123123
ImportDirectiveSubclass::ExternCrate => {
124-
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
125-
let msg = "unused extern crate";
126-
; resolver.session.buffer_lint(lint, directive.id, directive.span, msg)
124+
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
127125
}
128126
ImportDirectiveSubclass::MacroUse => {
129127
let lint = lint::builtin::UNUSED_IMPORTS;

src/librustc_resolve/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ pub struct Resolver<'a> {
12501250

12511251
used_imports: FxHashSet<(NodeId, Namespace)>,
12521252
pub maybe_unused_trait_imports: NodeSet,
1253+
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
12531254

12541255
/// privacy errors are delayed until the end in order to deduplicate them
12551256
privacy_errors: Vec<PrivacyError<'a>>,
@@ -1457,6 +1458,7 @@ impl<'a> Resolver<'a> {
14571458

14581459
used_imports: FxHashSet(),
14591460
maybe_unused_trait_imports: NodeSet(),
1461+
maybe_unused_extern_crates: Vec::new(),
14601462

14611463
privacy_errors: Vec::new(),
14621464
ambiguity_errors: Vec::new(),

src/librustc_tsan/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ build_helper = { path = "../build_helper" }
1414
cmake = "0.1.18"
1515

1616
[dependencies]
17+
alloc = { path = "../liballoc" }
1718
alloc_system = { path = "../liballoc_system" }
1819
core = { path = "../libcore" }

src/librustc_tsan/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
// except according to those terms.
1010

1111
#![sanitizer_runtime]
12-
#![feature(sanitizer_runtime)]
1312
#![feature(alloc_system)]
13+
#![feature(allocator_api)]
14+
#![feature(global_allocator)]
15+
#![feature(sanitizer_runtime)]
1416
#![feature(staged_api)]
1517
#![no_std]
1618
#![unstable(feature = "sanitizer_runtime_lib",
1719
reason = "internal implementation detail of sanitizers",
1820
issue = "0")]
1921

2022
extern crate alloc_system;
23+
24+
use alloc_system::System;
25+
26+
#[global_allocator]
27+
static ALLOC: System = System;

src/librustc_typeck/check_unused.rs

+11
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,15 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
7272

7373
let mut visitor = CheckVisitor { tcx, used_trait_imports };
7474
tcx.hir.krate().visit_all_item_likes(&mut visitor);
75+
76+
for &(id, span) in &tcx.maybe_unused_extern_crates {
77+
let cnum = tcx.sess.cstore.extern_mod_stmt_cnum(id).unwrap().as_def_id();
78+
if !tcx.is_compiler_builtins(cnum)
79+
&& !tcx.is_panic_runtime(cnum)
80+
&& !tcx.has_global_allocator(cnum) {
81+
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
82+
let msg = "unused extern crate";
83+
tcx.lint_node(lint, id, span, msg);
84+
}
85+
}
7586
}

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ extern crate std_unicode;
358358
extern crate libc;
359359

360360
// We always need an unwinder currently for backtraces
361+
#[allow(unused_extern_crates)]
361362
extern crate unwind;
362363

363364
// compiler-rt intrinsics

src/libstd/prelude/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//! On a technical level, Rust inserts
2424
//!
2525
//! ```
26+
//! # #[allow(unused_extern_crates)]
2627
//! extern crate std;
2728
//! ```
2829
//!

src/libstd/sys/unix/ext/fs.rs

-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ pub trait OpenOptionsExt {
154154
/// # Examples
155155
///
156156
/// ```no_run
157-
/// # #![feature(libc)]
158-
/// extern crate libc;
159157
/// use std::fs::OpenOptions;
160158
/// use std::os::unix::fs::OpenOptionsExt;
161159
///

src/libsyntax_ext/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ crate-type = ["dylib"]
1010

1111
[dependencies]
1212
fmt_macros = { path = "../libfmt_macros" }
13-
log = "0.3"
1413
proc_macro = { path = "../libproc_macro" }
1514
rustc_errors = { path = "../librustc_errors" }
1615
syntax = { path = "../libsyntax" }

src/libsyntax_ext/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#![feature(proc_macro_internals)]
1919

2020
extern crate fmt_macros;
21-
extern crate log;
2221
#[macro_use]
2322
extern crate syntax;
2423
extern crate syntax_pos;

0 commit comments

Comments
 (0)