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

@ -23,10 +23,13 @@ use rustc_data_structures::sync::{self, Lock, Lrc};
use rustc_data_structures::AtomicRef;
use rustc_lint_defs::FutureBreakage;
pub use rustc_lint_defs::{pluralize, Applicability};
use rustc_serialize::json::Json;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::source_map::SourceMap;
use rustc_span::{Loc, MultiSpan, Span};
use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use std::panic;
use std::path::Path;
use std::{error, fmt};
@ -73,6 +76,35 @@ impl SuggestionStyle {
}
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct ToolMetadata(pub Option<Json>);
impl ToolMetadata {
fn new(json: Json) -> Self {
ToolMetadata(Some(json))
}
}
impl Hash for ToolMetadata {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
// Doesn't really need to round-trip
impl<D: Decoder> Decodable<D> for ToolMetadata {
fn decode(_d: &mut D) -> Result<Self, D::Error> {
Ok(ToolMetadata(None))
}
}
impl<S: Encoder> Encodable<S> for ToolMetadata {
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
match &self.0 {
None => e.emit_unit(),
Some(json) => json.encode(e),
}
}
}
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
pub struct CodeSuggestion {
/// Each substitute can have multiple variants due to multiple
@ -106,6 +138,8 @@ pub struct CodeSuggestion {
/// which are useful for users but not useful for
/// tools like rustfix
pub applicability: Applicability,
/// Tool-specific metadata
pub tool_metadata: ToolMetadata,
}
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
@ -775,7 +809,6 @@ impl HandlerInner {
}
let already_emitted = |this: &mut Self| {
use std::hash::Hash;
let mut hasher = StableHasher::new();
diagnostic.hash(&mut hasher);
let diagnostic_hash = hasher.finish();