From 235523c7d4acdbd38a6b31c53b7969475d460e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 9 Feb 2019 03:39:08 -0800 Subject: [PATCH] Add way to completely hide suggestion from cli output --- src/librustc_errors/diagnostic.rs | 21 +++++++++++++++++++++ src/librustc_errors/diagnostic_builder.rs | 19 +++++++++++++++++++ src/librustc_errors/emitter.rs | 4 +++- src/librustc_errors/lib.rs | 6 ++++-- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index 8394e66850e8c..588cdfcb1afff 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -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>(&mut self, sp: S) -> &mut Self { self.span = sp.into(); self diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 5b37a7bb3467d..9f838987f0c19 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -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>(&mut self, sp: S) -> &mut Self); forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self); diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 6e41196099887..5e7c5315d6876 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -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)], diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index be959a29a5577..7805bf697c64a 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -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. @@ -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, } } }