Suggest removing the non-printing characters

This commit is contained in:
5225225 2021-11-13 12:46:22 +00:00
parent de05d3ec31
commit 52199c93bb
3 changed files with 24 additions and 9 deletions

View File

@ -7,6 +7,10 @@ use rustc_errors::{pluralize, Applicability, Handler};
use rustc_lexer::unescape::{EscapeError, Mode};
use rustc_span::{BytePos, Span};
fn printing(ch: char) -> bool {
unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) != 0 && !ch.is_whitespace()
}
pub(crate) fn emit_unescape_error(
handler: &Handler,
// interior part of the literal, without quotes
@ -83,7 +87,11 @@ pub(crate) fn emit_unescape_error(
);
}
} else {
if lit.chars().filter(|x| x.is_whitespace() || x.is_control()).count() >= 1 {
let printable: Vec<char> = lit.chars().filter(|x| printing(*x)).collect();
if let [ch] = printable.as_slice() {
has_help = true;
handler.span_note(
span,
&format!(
@ -91,6 +99,13 @@ pub(crate) fn emit_unescape_error(
lit.escape_default(),
),
);
handler.span_suggestion(
span,
"consider removing the non-printing characters",
ch.to_string(),
Applicability::MaybeIncorrect,
);
}
}

View File

@ -2,8 +2,9 @@
// characters in it contains a note about non-printing characters.
fn main() {
// <hair space>x<zero width space>
let _hair_space_around = 'x';
//~^ ERROR: character literal may only contain one codepoint
//~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
//~| HELP: consider removing the non-printing characters
//~| SUGGESTION: x
}

View File

@ -1,18 +1,17 @@
['x']
error: character literal may only contain one codepoint
--> $DIR/whitespace-character-literal.rs:6:30
--> $DIR/whitespace-character-literal.rs:5:30
|
LL | let _hair_space_around = 'x';
| ^^^^
| ^--^
| |
| help: consider removing the non-printing characters: `x`
|
note: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
--> $DIR/whitespace-character-literal.rs:6:31
--> $DIR/whitespace-character-literal.rs:5:31
|
LL | let _hair_space_around = 'x';
| ^^
help: if you meant to write a `str` literal, use double quotes
|
LL | let _hair_space_around = "x";
| ~~~~
error: aborting due to previous error