Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Emit unused externs
  • Loading branch information
est31 committed Mar 8, 2021
1 parent 76c500e commit aef1e35
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_errors/src/emitter.rs
Expand Up @@ -195,6 +195,9 @@ pub trait Emitter {

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

/// Emit list of unused externs
fn emit_unused_externs(&mut self, _unused_externs: &[&str]) {}

/// Checks if should show explanations about "rustc --explain"
fn should_show_explain(&self) -> bool {
true
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_errors/src/json.rs
Expand Up @@ -159,6 +159,19 @@ impl Emitter for JsonEmitter {
}
}

fn emit_unused_externs(&mut self, unused_externs: &[&str]) {
let data = UnusedExterns { unused_extern_names: unused_externs };
let result = if self.pretty {
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
} else {
writeln!(&mut self.dst, "{}", as_json(&data))
}
.and_then(|_| self.dst.flush());
if let Err(e) = result {
panic!("failed to print unused externs: {:?}", e);
}
}

fn source_map(&self) -> Option<&Lrc<SourceMap>> {
Some(&self.sm)
}
Expand Down Expand Up @@ -322,6 +335,12 @@ struct FutureIncompatReport {
future_incompat_report: Vec<FutureBreakageItem>,
}

#[derive(Encodable)]
struct UnusedExterns<'a, 'b> {
/// List of unused externs by their names.
unused_extern_names: &'a [&'b str],
}

impl Diagnostic {
fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
let sugg = diag.suggestions.iter().map(|sugg| Diagnostic {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Expand Up @@ -767,6 +767,10 @@ impl Handler {
self.inner.borrow_mut().emitter.emit_future_breakage_report(diags)
}

pub fn emit_unused_externs(&self, unused_externs: &[&str]) {
self.inner.borrow_mut().emit_unused_externs(unused_externs)
}

pub fn delay_as_bug(&self, diagnostic: Diagnostic) {
self.inner.borrow_mut().delay_as_bug(diagnostic)
}
Expand Down Expand Up @@ -841,6 +845,10 @@ impl HandlerInner {
self.emitter.emit_artifact_notification(path, artifact_type);
}

fn emit_unused_externs(&mut self, unused_externs: &[&str]) {
self.emitter.emit_unused_externs(unused_externs);
}

fn treat_err_as_bug(&self) -> bool {
self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() >= c.get())
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_metadata/src/creader.rs
Expand Up @@ -893,6 +893,7 @@ impl<'a> CrateLoader<'a> {
fn report_unused_deps(&mut self, krate: &ast::Crate) {
// Make a point span rather than covering the whole file
let span = krate.span.shrink_to_lo();
let mut unused_externs = Vec::new();
// Complain about anything left over
for (name, entry) in self.sess.opts.externs.iter() {
if let ExternLocation::FoundInLibrarySearchDirectories = entry.location {
Expand All @@ -917,6 +918,7 @@ impl<'a> CrateLoader<'a> {
)
}
};
unused_externs.push(name as &str);
self.sess.parse_sess.buffer_lint_with_diagnostic(
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
span,
Expand All @@ -929,6 +931,8 @@ impl<'a> CrateLoader<'a> {
diag,
);
}
// FIXME: add gating
self.sess.parse_sess.span_diagnostic.emit_unused_externs(&unused_externs);
}

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

0 comments on commit aef1e35

Please sign in to comment.