1
Fork 0

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_lexer::unescape::{EscapeError, Mode};
use rustc_span::{BytePos, Span}; 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( pub(crate) fn emit_unescape_error(
handler: &Handler, handler: &Handler,
// interior part of the literal, without quotes // interior part of the literal, without quotes
@ -83,7 +87,11 @@ pub(crate) fn emit_unescape_error(
); );
} }
} else { } 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( handler.span_note(
span, span,
&format!( &format!(
@ -91,6 +99,13 @@ pub(crate) fn emit_unescape_error(
lit.escape_default(), 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. // characters in it contains a note about non-printing characters.
fn main() { fn main() {
// <hair space>x<zero width space>
let _hair_space_around = 'x'; let _hair_space_around = 'x';
//~^ ERROR: character literal may only contain one codepoint //~^ ERROR: character literal may only contain one codepoint
//~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}` //~| 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 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'; 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}` 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'; 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 error: aborting due to previous error