Skip to content

Commit 65e02be

Browse files
committed
Suggest removing the non-printing characters
1 parent da7dd4f commit 65e02be

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use rustc_errors::{pluralize, Applicability, Handler};
77
use rustc_lexer::unescape::{EscapeError, Mode};
88
use rustc_span::{BytePos, Span};
99

10+
fn printing(ch: char) -> bool {
11+
unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) != 0 && !ch.is_whitespace()
12+
}
13+
1014
pub(crate) fn emit_unescape_error(
1115
handler: &Handler,
1216
// interior part of the literal, without quotes
@@ -83,14 +87,25 @@ pub(crate) fn emit_unescape_error(
8387
);
8488
}
8589
} else {
86-
if lit.chars().filter(|x| x.is_whitespace() || x.is_control()).count() >= 1 {
90+
let printable: Vec<char> = lit.chars().filter(|x| printing(*x)).collect();
91+
92+
if let [ch] = printable.as_slice() {
93+
has_help = true;
94+
8795
handler.span_note(
8896
span,
8997
&format!(
9098
"there are non-printing characters, the full sequence is `{}`",
9199
lit.escape_default(),
92100
),
93101
);
102+
103+
handler.span_suggestion(
104+
span,
105+
"consider removing the non-printing characters",
106+
ch.to_string(),
107+
Applicability::MaybeIncorrect,
108+
);
94109
}
95110
}
96111

src/test/ui/parser/char/whitespace-character-literal.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// characters in it contains a note about non-printing characters.
33

44
fn main() {
5-
// <hair space>x<zero width space>
65
let _hair_space_around = ' x​';
76
//~^ ERROR: character literal may only contain one codepoint
87
//~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
8+
//~| HELP: consider removing the non-printing characters
9+
//~| SUGGESTION: x
910
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
['x']
12
error: character literal may only contain one codepoint
2-
--> $DIR/whitespace-character-literal.rs:6:30
3+
--> $DIR/whitespace-character-literal.rs:5:30
34
|
45
LL | let _hair_space_around = ' x​';
5-
| ^^^^
6+
| ^--^
7+
| |
8+
| help: consider removing the non-printing characters: `x`
69
|
710
note: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}`
8-
--> $DIR/whitespace-character-literal.rs:6:31
11+
--> $DIR/whitespace-character-literal.rs:5:31
912
|
1013
LL | let _hair_space_around = ' x​';
1114
| ^^
12-
help: if you meant to write a `str` literal, use double quotes
13-
|
14-
LL | let _hair_space_around = " x​";
15-
| ~~~~
1615

1716
error: aborting due to previous error
1817

0 commit comments

Comments
 (0)