1
Fork 0

Add --extern-loc to augment unused crate dependency diagnostics

This allows a build system to indicate a location in its own dependency
specification files (eg Cargo's `Cargo.toml`) which can be reported
along side any unused crate dependency.

This supports several types of location:
 - 'json' - provide some json-structured data, which is included in the json diagnostics
     in a `tool_metadata` field
 - 'raw' - emit the provided string into the output. This also appears as a json string in
     `tool_metadata`.

If no `--extern-location` is explicitly provided then a default json entry of the form
`"tool_metadata":{"name":<cratename>,"path":<cratepath>}` is emitted.
This commit is contained in:
Jeremy Fitzhardinge 2020-05-25 16:21:25 -07:00
parent 9778068cbc
commit 82ccb6582a
44 changed files with 512 additions and 47 deletions

View file

@ -16,8 +16,9 @@ use rustc_index::vec::IndexVec;
use rustc_middle::middle::cstore::{CrateDepKind, CrateSource, ExternCrate};
use rustc_middle::middle::cstore::{ExternCrateSource, MetadataLoaderDyn};
use rustc_middle::ty::TyCtxt;
use rustc_serialize::json::ToJson;
use rustc_session::config::{self, CrateType, ExternLocation};
use rustc_session::lint;
use rustc_session::lint::{self, BuiltinLintDiagnostics, ExternDepSpec};
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_session::{CrateDisambiguator, Session};
@ -27,6 +28,7 @@ use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::{PanicStrategy, TargetTriple};
use proc_macro::bridge::client::ProcMacro;
use std::collections::BTreeMap;
use std::path::Path;
use std::{cmp, env};
use tracing::{debug, info};
@ -871,8 +873,25 @@ impl<'a> CrateLoader<'a> {
// Don't worry about pathless `--extern foo` sysroot references
continue;
}
if !self.used_extern_options.contains(&Symbol::intern(name)) {
self.sess.parse_sess.buffer_lint(
if self.used_extern_options.contains(&Symbol::intern(name)) {
continue;
}
// Got a real unused --extern
let diag = match self.sess.opts.extern_dep_specs.get(name) {
Some(loc) => BuiltinLintDiagnostics::ExternDepSpec(name.clone(), loc.into()),
None => {
// If we don't have a specific location, provide a json encoding of the `--extern`
// option.
let meta: BTreeMap<String, String> =
std::iter::once(("name".to_string(), name.to_string())).collect();
BuiltinLintDiagnostics::ExternDepSpec(
name.clone(),
ExternDepSpec::Json(meta.to_json()),
)
}
};
self.sess.parse_sess.buffer_lint_with_diagnostic(
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
span,
ast::CRATE_NODE_ID,
@ -881,8 +900,8 @@ impl<'a> CrateLoader<'a> {
name,
self.local_crate_name,
name),
diag,
);
}
}
}