Skip to content

Commit

Permalink
Add way to completely hide suggestion from cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 11, 2019
1 parent 7cfba1c commit 235523c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/librustc_errors/diagnostic.rs
Expand Up @@ -346,6 +346,27 @@ impl Diagnostic {
self
}

/// Adds a suggestion to the json output, but otherwise remains silent/undisplayed in the cli.
///
/// This is intended to be used for suggestions that are *very* obvious in what the changes
/// need to be from the message, but we still want other tools to be able to apply them.
pub fn tool_only_span_suggestion(
&mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
) -> &mut Self {
self.suggestions.push(CodeSuggestion {
substitutions: vec![Substitution {
parts: vec![SubstitutionPart {
snippet: suggestion,
span: sp,
}],
}],
msg: msg.to_owned(),
style: SuggestionStyle::CompletelyHidden,
applicability: applicability,
});
self
}

pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
self.span = sp.into();
self
Expand Down
19 changes: 19 additions & 0 deletions src/librustc_errors/diagnostic_builder.rs
Expand Up @@ -281,6 +281,25 @@ impl<'a> DiagnosticBuilder<'a> {
self
}

pub fn tool_only_span_suggestion(
&mut self,
sp: Span,
msg: &str,
suggestion: String,
applicability: Applicability,
) -> &mut Self {
if !self.allow_suggestions {
return self
}
self.diagnostic.tool_only_span_suggestion(
sp,
msg,
suggestion,
applicability,
);
self
}

forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_errors/emitter.rs
Expand Up @@ -1363,7 +1363,9 @@ impl EmitterWriter {
}
}
for sugg in suggestions {
if sugg.style == SuggestionStyle::HideCodeAlways {
if sugg.style == SuggestionStyle::CompletelyHidden {
// do not display this suggestion, it is meant only for tools
} else if sugg.style == SuggestionStyle::HideCodeAlways {
match self.emit_message_default(
&MultiSpan::new(),
&[(sugg.msg.to_owned(), Style::HeaderMsg)],
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_errors/lib.rs
Expand Up @@ -72,8 +72,10 @@ pub enum Applicability {
pub enum SuggestionStyle {
/// Hide the suggested code when displaying this suggestion inline.
HideCodeInline,
/// Always hide the suggested code.
/// Always hide the suggested code but display the message.
HideCodeAlways,
/// Do not display this suggestion in the cli output, it is only meant for tools.
CompletelyHidden,
/// Always show the suggested code.
/// This will *not* show the code if the suggestion is inline *and* the suggested code is
/// empty.
Expand All @@ -83,8 +85,8 @@ pub enum SuggestionStyle {
impl SuggestionStyle {
fn hide_inline(&self) -> bool {
match *self {
SuggestionStyle::HideCodeAlways | SuggestionStyle::HideCodeInline => true,
SuggestionStyle::ShowCode => false,
_ => true,
}
}
}
Expand Down

0 comments on commit 235523c

Please sign in to comment.