Skip to content

Commit 235523c

Browse files
committed
Add way to completely hide suggestion from cli output
1 parent 7cfba1c commit 235523c

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/librustc_errors/diagnostic.rs

+21
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,27 @@ impl Diagnostic {
346346
self
347347
}
348348

349+
/// Adds a suggestion to the json output, but otherwise remains silent/undisplayed in the cli.
350+
///
351+
/// This is intended to be used for suggestions that are *very* obvious in what the changes
352+
/// need to be from the message, but we still want other tools to be able to apply them.
353+
pub fn tool_only_span_suggestion(
354+
&mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
355+
) -> &mut Self {
356+
self.suggestions.push(CodeSuggestion {
357+
substitutions: vec![Substitution {
358+
parts: vec![SubstitutionPart {
359+
snippet: suggestion,
360+
span: sp,
361+
}],
362+
}],
363+
msg: msg.to_owned(),
364+
style: SuggestionStyle::CompletelyHidden,
365+
applicability: applicability,
366+
});
367+
self
368+
}
369+
349370
pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
350371
self.span = sp.into();
351372
self

src/librustc_errors/diagnostic_builder.rs

+19
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,25 @@ impl<'a> DiagnosticBuilder<'a> {
281281
self
282282
}
283283

284+
pub fn tool_only_span_suggestion(
285+
&mut self,
286+
sp: Span,
287+
msg: &str,
288+
suggestion: String,
289+
applicability: Applicability,
290+
) -> &mut Self {
291+
if !self.allow_suggestions {
292+
return self
293+
}
294+
self.diagnostic.tool_only_span_suggestion(
295+
sp,
296+
msg,
297+
suggestion,
298+
applicability,
299+
);
300+
self
301+
}
302+
284303
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
285304
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
286305

src/librustc_errors/emitter.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,9 @@ impl EmitterWriter {
13631363
}
13641364
}
13651365
for sugg in suggestions {
1366-
if sugg.style == SuggestionStyle::HideCodeAlways {
1366+
if sugg.style == SuggestionStyle::CompletelyHidden {
1367+
// do not display this suggestion, it is meant only for tools
1368+
} else if sugg.style == SuggestionStyle::HideCodeAlways {
13671369
match self.emit_message_default(
13681370
&MultiSpan::new(),
13691371
&[(sugg.msg.to_owned(), Style::HeaderMsg)],

src/librustc_errors/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ pub enum Applicability {
7272
pub enum SuggestionStyle {
7373
/// Hide the suggested code when displaying this suggestion inline.
7474
HideCodeInline,
75-
/// Always hide the suggested code.
75+
/// Always hide the suggested code but display the message.
7676
HideCodeAlways,
77+
/// Do not display this suggestion in the cli output, it is only meant for tools.
78+
CompletelyHidden,
7779
/// Always show the suggested code.
7880
/// This will *not* show the code if the suggestion is inline *and* the suggested code is
7981
/// empty.
@@ -83,8 +85,8 @@ pub enum SuggestionStyle {
8385
impl SuggestionStyle {
8486
fn hide_inline(&self) -> bool {
8587
match *self {
86-
SuggestionStyle::HideCodeAlways | SuggestionStyle::HideCodeInline => true,
8788
SuggestionStyle::ShowCode => false,
89+
_ => true,
8890
}
8991
}
9092
}

0 commit comments

Comments
 (0)