Skip to content

Commit aef1e35

Browse files
committed
Emit unused externs
1 parent 76c500e commit aef1e35

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ pub trait Emitter {
195195

196196
fn emit_future_breakage_report(&mut self, _diags: Vec<(FutureBreakage, Diagnostic)>) {}
197197

198+
/// Emit list of unused externs
199+
fn emit_unused_externs(&mut self, _unused_externs: &[&str]) {}
200+
198201
/// Checks if should show explanations about "rustc --explain"
199202
fn should_show_explain(&self) -> bool {
200203
true

compiler/rustc_errors/src/json.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ impl Emitter for JsonEmitter {
159159
}
160160
}
161161

162+
fn emit_unused_externs(&mut self, unused_externs: &[&str]) {
163+
let data = UnusedExterns { unused_extern_names: unused_externs };
164+
let result = if self.pretty {
165+
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
166+
} else {
167+
writeln!(&mut self.dst, "{}", as_json(&data))
168+
}
169+
.and_then(|_| self.dst.flush());
170+
if let Err(e) = result {
171+
panic!("failed to print unused externs: {:?}", e);
172+
}
173+
}
174+
162175
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
163176
Some(&self.sm)
164177
}
@@ -322,6 +335,12 @@ struct FutureIncompatReport {
322335
future_incompat_report: Vec<FutureBreakageItem>,
323336
}
324337

338+
#[derive(Encodable)]
339+
struct UnusedExterns<'a, 'b> {
340+
/// List of unused externs by their names.
341+
unused_extern_names: &'a [&'b str],
342+
}
343+
325344
impl Diagnostic {
326345
fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
327346
let sugg = diag.suggestions.iter().map(|sugg| Diagnostic {

compiler/rustc_errors/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ impl Handler {
767767
self.inner.borrow_mut().emitter.emit_future_breakage_report(diags)
768768
}
769769

770+
pub fn emit_unused_externs(&self, unused_externs: &[&str]) {
771+
self.inner.borrow_mut().emit_unused_externs(unused_externs)
772+
}
773+
770774
pub fn delay_as_bug(&self, diagnostic: Diagnostic) {
771775
self.inner.borrow_mut().delay_as_bug(diagnostic)
772776
}
@@ -841,6 +845,10 @@ impl HandlerInner {
841845
self.emitter.emit_artifact_notification(path, artifact_type);
842846
}
843847

848+
fn emit_unused_externs(&mut self, unused_externs: &[&str]) {
849+
self.emitter.emit_unused_externs(unused_externs);
850+
}
851+
844852
fn treat_err_as_bug(&self) -> bool {
845853
self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() >= c.get())
846854
}

compiler/rustc_metadata/src/creader.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ impl<'a> CrateLoader<'a> {
893893
fn report_unused_deps(&mut self, krate: &ast::Crate) {
894894
// Make a point span rather than covering the whole file
895895
let span = krate.span.shrink_to_lo();
896+
let mut unused_externs = Vec::new();
896897
// Complain about anything left over
897898
for (name, entry) in self.sess.opts.externs.iter() {
898899
if let ExternLocation::FoundInLibrarySearchDirectories = entry.location {
@@ -917,6 +918,7 @@ impl<'a> CrateLoader<'a> {
917918
)
918919
}
919920
};
921+
unused_externs.push(name as &str);
920922
self.sess.parse_sess.buffer_lint_with_diagnostic(
921923
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
922924
span,
@@ -929,6 +931,8 @@ impl<'a> CrateLoader<'a> {
929931
diag,
930932
);
931933
}
934+
// FIXME: add gating
935+
self.sess.parse_sess.span_diagnostic.emit_unused_externs(&unused_externs);
932936
}
933937

934938
pub fn postprocess(&mut self, krate: &ast::Crate) {

0 commit comments

Comments
 (0)