From faf90351b79de04de0176b1e0de07cc5f1730eaa Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 16 Jul 2017 11:43:24 -0700 Subject: Add flag to hide code on inline suggestions Now there's a way to add suggestions that hide the suggested code when presented inline, to avoid weird wording when short code snippets are added at the end. --- src/librustc_errors/diagnostic.rs | 18 ++++++++++++++++++ src/librustc_errors/diagnostic_builder.rs | 5 +++++ src/librustc_errors/emitter.rs | 5 +++-- src/librustc_errors/lib.rs | 1 + 4 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index d7c21127474..2ffa5fc796e 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -209,6 +209,22 @@ impl Diagnostic { self } + /// Prints out a message with a suggested edit of the code. If the suggestion is presented + /// inline it will only show the text message and not the text. + /// + /// See `diagnostic::CodeSuggestion` for more information. + pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self { + self.suggestions.push(CodeSuggestion { + substitution_parts: vec![Substitution { + span: sp, + substitutions: vec![suggestion], + }], + msg: msg.to_owned(), + show_code_when_inline: false, + }); + self + } + /// Prints out a message with a suggested edit of the code. /// /// See `diagnostic::CodeSuggestion` for more information. @@ -219,6 +235,7 @@ impl Diagnostic { substitutions: vec![suggestion], }], msg: msg.to_owned(), + show_code_when_inline: true, }); self } @@ -230,6 +247,7 @@ impl Diagnostic { substitutions: suggestions, }], msg: msg.to_owned(), + show_code_when_inline: true, }); self } diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 0081339a363..6f6470089d7 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -146,6 +146,11 @@ impl<'a> DiagnosticBuilder<'a> { sp: S, msg: &str) -> &mut Self); + forward!(pub fn span_suggestion_short(&mut self, + sp: Span, + msg: &str, + suggestion: String) + -> &mut Self); forward!(pub fn span_suggestion(&mut self, sp: Span, msg: &str, diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 2aea6d125f2..94b3a4396d7 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -47,8 +47,9 @@ impl Emitter for EmitterWriter { // don't display multiline suggestions as labels sugg.substitution_parts[0].substitutions[0].find('\n').is_none() { let substitution = &sugg.substitution_parts[0].substitutions[0]; - let msg = if substitution.len() == 0 { - // This substitution is only removal, don't show it + let msg = if substitution.len() == 0 || !sugg.show_code_when_inline { + // This substitution is only removal or we explicitely don't want to show the + // code inline, don't show it format!("help: {}", sugg.msg) } else { format!("help: {} `{}`", sugg.msg, substitution) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index c4beb5ebc42..e873137444d 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -84,6 +84,7 @@ pub struct CodeSuggestion { /// ``` pub substitution_parts: Vec, pub msg: String, + pub show_code_when_inline: bool, } #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)] -- cgit 1.4.1-3-g733a5