Skip to content

Commit

Permalink
Add flag to hide code on inline suggestions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
estebank committed Jul 17, 2017
1 parent 7239d77 commit faf9035
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/librustc_errors/diagnostic.rs
Expand Up @@ -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.
Expand All @@ -219,6 +235,7 @@ impl Diagnostic {
substitutions: vec![suggestion],
}],
msg: msg.to_owned(),
show_code_when_inline: true,
});
self
}
Expand All @@ -230,6 +247,7 @@ impl Diagnostic {
substitutions: suggestions,
}],
msg: msg.to_owned(),
show_code_when_inline: true,
});
self
}
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_errors/diagnostic_builder.rs
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_errors/emitter.rs
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/librustc_errors/lib.rs
Expand Up @@ -84,6 +84,7 @@ pub struct CodeSuggestion {
/// ```
pub substitution_parts: Vec<Substitution>,
pub msg: String,
pub show_code_when_inline: bool,
}

#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -2807,9 +2807,9 @@ impl<'a> Parser<'a> {
let cur_pos = cm.lookup_char_pos(self.span.lo);
let op_pos = cm.lookup_char_pos(cur_op_span.hi);
if cur_pos.line != op_pos.line {
err.span_suggestion(cur_op_span,
"did you mean to end the statement here instead?",
";".to_string());
err.span_suggestion_short(cur_op_span,
"did you mean to use `;` here?",
";".to_string());
}
return Err(err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-22644.stderr
Expand Up @@ -100,5 +100,5 @@ error: expected type, found `4`
--> $DIR/issue-22644.rs:38:28
|
38 | println!("{}", a: &mut 4);
| ^
| ^ expecting a type here because of type ascription

Expand Up @@ -2,7 +2,7 @@ error: expected type, found `0`
--> $DIR/type-ascription-instead-of-statement-end.rs:15:5
|
14 | println!("test"):
| - help: did you mean to end the statement here instead? `;`
| - help: did you mean to use `;` here?
15 | 0;
| ^ expecting a type here because of type ascription

Expand Down

0 comments on commit faf9035

Please sign in to comment.