Skip to content

Commit faf9035

Browse files
committed
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.
1 parent 7239d77 commit faf9035

File tree

7 files changed

+32
-7
lines changed

7 files changed

+32
-7
lines changed

src/librustc_errors/diagnostic.rs

+18
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ impl Diagnostic {
209209
self
210210
}
211211

212+
/// Prints out a message with a suggested edit of the code. If the suggestion is presented
213+
/// inline it will only show the text message and not the text.
214+
///
215+
/// See `diagnostic::CodeSuggestion` for more information.
216+
pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
217+
self.suggestions.push(CodeSuggestion {
218+
substitution_parts: vec![Substitution {
219+
span: sp,
220+
substitutions: vec![suggestion],
221+
}],
222+
msg: msg.to_owned(),
223+
show_code_when_inline: false,
224+
});
225+
self
226+
}
227+
212228
/// Prints out a message with a suggested edit of the code.
213229
///
214230
/// See `diagnostic::CodeSuggestion` for more information.
@@ -219,6 +235,7 @@ impl Diagnostic {
219235
substitutions: vec![suggestion],
220236
}],
221237
msg: msg.to_owned(),
238+
show_code_when_inline: true,
222239
});
223240
self
224241
}
@@ -230,6 +247,7 @@ impl Diagnostic {
230247
substitutions: suggestions,
231248
}],
232249
msg: msg.to_owned(),
250+
show_code_when_inline: true,
233251
});
234252
self
235253
}

src/librustc_errors/diagnostic_builder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ impl<'a> DiagnosticBuilder<'a> {
146146
sp: S,
147147
msg: &str)
148148
-> &mut Self);
149+
forward!(pub fn span_suggestion_short(&mut self,
150+
sp: Span,
151+
msg: &str,
152+
suggestion: String)
153+
-> &mut Self);
149154
forward!(pub fn span_suggestion(&mut self,
150155
sp: Span,
151156
msg: &str,

src/librustc_errors/emitter.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ impl Emitter for EmitterWriter {
4747
// don't display multiline suggestions as labels
4848
sugg.substitution_parts[0].substitutions[0].find('\n').is_none() {
4949
let substitution = &sugg.substitution_parts[0].substitutions[0];
50-
let msg = if substitution.len() == 0 {
51-
// This substitution is only removal, don't show it
50+
let msg = if substitution.len() == 0 || !sugg.show_code_when_inline {
51+
// This substitution is only removal or we explicitely don't want to show the
52+
// code inline, don't show it
5253
format!("help: {}", sugg.msg)
5354
} else {
5455
format!("help: {} `{}`", sugg.msg, substitution)

src/librustc_errors/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct CodeSuggestion {
8484
/// ```
8585
pub substitution_parts: Vec<Substitution>,
8686
pub msg: String,
87+
pub show_code_when_inline: bool,
8788
}
8889

8990
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]

src/libsyntax/parse/parser.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2807,9 +2807,9 @@ impl<'a> Parser<'a> {
28072807
let cur_pos = cm.lookup_char_pos(self.span.lo);
28082808
let op_pos = cm.lookup_char_pos(cur_op_span.hi);
28092809
if cur_pos.line != op_pos.line {
2810-
err.span_suggestion(cur_op_span,
2811-
"did you mean to end the statement here instead?",
2812-
";".to_string());
2810+
err.span_suggestion_short(cur_op_span,
2811+
"did you mean to use `;` here?",
2812+
";".to_string());
28132813
}
28142814
return Err(err);
28152815
}

src/test/ui/issue-22644.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ error: expected type, found `4`
100100
--> $DIR/issue-22644.rs:38:28
101101
|
102102
38 | println!("{}", a: &mut 4);
103-
| ^
103+
| ^ expecting a type here because of type ascription
104104

src/test/ui/suggestions/type-ascription-instead-of-statement-end.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected type, found `0`
22
--> $DIR/type-ascription-instead-of-statement-end.rs:15:5
33
|
44
14 | println!("test"):
5-
| - help: did you mean to end the statement here instead? `;`
5+
| - help: did you mean to use `;` here?
66
15 | 0;
77
| ^ expecting a type here because of type ascription
88

0 commit comments

Comments
 (0)