1
Fork 0

Use structured suggestion for braceless unicode escape squence

This commit is contained in:
Esteban Küber 2018-12-27 11:21:47 -08:00
parent 7edc434b72
commit b416f1398f
5 changed files with 47 additions and 16 deletions

View file

@ -945,12 +945,36 @@ impl<'a> StringReader<'a> {
self.scan_unicode_escape(delim) && !ascii_only
} else {
let span = self.mk_sp(start, self.pos);
self.sess.span_diagnostic
.struct_span_err(span, "incorrect unicode escape sequence")
.span_help(span,
"format of unicode escape sequences is \
`\\u{}`")
.emit();
let mut suggestion = "\\u{".to_owned();
let mut err = self.sess.span_diagnostic.struct_span_err(
span,
"incorrect unicode escape sequence",
);
let mut i = 0;
while let (Some(ch), true) = (self.ch, i < 6) {
if ch.is_digit(16) {
suggestion.push(ch);
self.bump();
i += 1;
} else {
break;
}
}
if i != 0 {
suggestion.push('}');
err.span_suggestion_with_applicability(
self.mk_sp(start, self.pos),
"format of unicode escape sequences uses braces",
suggestion,
Applicability::MaybeIncorrect,
);
} else {
err.span_help(
span,
"format of unicode escape sequences is `\\u{…}`",
);
}
err.emit();
false
};
if ascii_only {

View file

@ -845,8 +845,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
}
} else if next_c.is_digit(16) {
skips.push(next_pos);
// We suggest adding `{` and `}` when appropriate, accept it here as if it
// were correct
// We suggest adding `{` and `}` when appropriate, accept it here as if
// it were correct
let mut i = 0; // consume up to 6 hexanumeric chars
while let (Some((next_pos, c)), _) = (s.next(), i < 6) {
if c.is_digit(16) {

View file

@ -2,13 +2,9 @@ error: incorrect unicode escape sequence
--> $DIR/format-string-error-2.rs:77:20
|
LL | println!("/x7B}/u8 {", 1);
| ^^
|
help: format of unicode escape sequences is `/u{…}`
--> $DIR/format-string-error-2.rs:77:20
|
LL | println!("/x7B}/u8 {", 1);
| ^^
| ^^-
| |
| help: format of unicode escape sequences uses braces: `/u{8}`
error: invalid format string: expected `'}'`, found `'a'`
--> $DIR/format-string-error-2.rs:5:5

View file

@ -35,4 +35,7 @@ fn main() {
//~^ ERROR invalid character in numeric character escape:
//~^^ ERROR form of character escape may only be used with characters in the range [\x00-\x7f]
//~^^^ ERROR incorrect unicode escape sequence
let _ = "\u8f";
//~^ ERROR incorrect unicode escape sequence
}

View file

@ -118,5 +118,13 @@ help: format of unicode escape sequences is `/u{…}`
LL | let _ = "/xf /u";
| ^^
error: aborting due to 17 previous errors
error: incorrect unicode escape sequence
--> $DIR/issue-23620-invalid-escapes.rs:39:14
|
LL | let _ = "/u8f";
| ^^--
| |
| help: format of unicode escape sequences uses braces: `/u{8f}`
error: aborting due to 18 previous errors