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 self.scan_unicode_escape(delim) && !ascii_only
} else { } else {
let span = self.mk_sp(start, self.pos); let span = self.mk_sp(start, self.pos);
self.sess.span_diagnostic let mut suggestion = "\\u{".to_owned();
.struct_span_err(span, "incorrect unicode escape sequence") let mut err = self.sess.span_diagnostic.struct_span_err(
.span_help(span, span,
"format of unicode escape sequences is \ "incorrect unicode escape sequence",
`\\u{…}`") );
.emit(); 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 false
}; };
if ascii_only { 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) { } else if next_c.is_digit(16) {
skips.push(next_pos); skips.push(next_pos);
// We suggest adding `{` and `}` when appropriate, accept it here as if it // We suggest adding `{` and `}` when appropriate, accept it here as if
// were correct // it were correct
let mut i = 0; // consume up to 6 hexanumeric chars let mut i = 0; // consume up to 6 hexanumeric chars
while let (Some((next_pos, c)), _) = (s.next(), i < 6) { while let (Some((next_pos, c)), _) = (s.next(), i < 6) {
if c.is_digit(16) { if c.is_digit(16) {

View File

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

View File

@@ -35,4 +35,7 @@ fn main() {
//~^ ERROR invalid character in numeric character escape: //~^ ERROR invalid character in numeric character escape:
//~^^ ERROR form of character escape may only be used with characters in the range [\x00-\x7f] //~^^ ERROR form of character escape may only be used with characters in the range [\x00-\x7f]
//~^^^ ERROR incorrect unicode escape sequence //~^^^ 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"; 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