about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/lexer
diff options
context:
space:
mode:
author5225225 <5225225@mailbox.org>2021-11-13 12:46:22 +0000
committer5225225 <5225225@mailbox.org>2021-11-16 08:06:30 +0000
commit52199c93bb065db6e4ca5b95977212a30c646b5f (patch)
tree38d909d603262a245ea62b4a98dfa70cb0a761f1 /compiler/rustc_parse/src/lexer
parentde05d3ec31f22b1f3afaf8b5ceddf224492c4aaa (diff)
downloadrust-52199c93bb065db6e4ca5b95977212a30c646b5f.tar.gz
rust-52199c93bb065db6e4ca5b95977212a30c646b5f.zip
Suggest removing the non-printing characters
Diffstat (limited to 'compiler/rustc_parse/src/lexer')
-rw-r--r--compiler/rustc_parse/src/lexer/unescape_error_reporting.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
index 0f6594a2a7f..aa7ab4a953c 100644
--- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
+++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
@@ -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,
+                    );
                 }
             }