about summary refs log tree commit diff
path: root/compiler/rustc_lexer/src
diff options
context:
space:
mode:
authorAnton Golov <jesyspa@gmail.com>2021-07-31 20:35:37 +0200
committerAnton Golov <jesyspa@gmail.com>2021-08-11 11:35:08 +0200
commita03fbfe2ff3e7dd03af42d337b11552e782e2dc4 (patch)
tree504e4d7aa5d4ab00343a3125d8e02bbcaaf071cd /compiler/rustc_lexer/src
parentd488de82f30fd1dcb0220d57498638596622394e (diff)
downloadrust-a03fbfe2ff3e7dd03af42d337b11552e782e2dc4.tar.gz
rust-a03fbfe2ff3e7dd03af42d337b11552e782e2dc4.zip
Warn when an escaped newline skips multiple lines
Diffstat (limited to 'compiler/rustc_lexer/src')
-rw-r--r--compiler/rustc_lexer/src/unescape.rs9
-rw-r--r--compiler/rustc_lexer/src/unescape/tests.rs5
2 files changed, 14 insertions, 0 deletions
diff --git a/compiler/rustc_lexer/src/unescape.rs b/compiler/rustc_lexer/src/unescape.rs
index 9a96c03cd3c..6550eed3958 100644
--- a/compiler/rustc_lexer/src/unescape.rs
+++ b/compiler/rustc_lexer/src/unescape.rs
@@ -60,6 +60,9 @@ pub enum EscapeError {
     /// After a line ending with '\', the next line contains whitespace
     /// characters that are not skipped.
     UnskippedWhitespaceWarning,
+
+    /// After a line ending with '\', multiple lines are skipped.
+    MultipleSkippedLinesWarning,
 }
 
 impl EscapeError {
@@ -67,6 +70,7 @@ impl EscapeError {
     pub fn is_fatal(&self) -> bool {
         match self {
             EscapeError::UnskippedWhitespaceWarning => false,
+            EscapeError::MultipleSkippedLinesWarning => false,
             _ => true,
         }
     }
@@ -320,6 +324,11 @@ where
             .bytes()
             .position(|b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r')
             .unwrap_or(str.len());
+        if str[1..first_non_space].contains('\n') {
+            // The +1 accounts for the escaping slash.
+            let end = start + first_non_space + 1;
+            callback(start..end, Err(EscapeError::MultipleSkippedLinesWarning));
+        }
         let tail = &str[first_non_space..];
         if let Some(c) = tail.chars().nth(0) {
             // For error reporting, we would like the span to contain the character that was not
diff --git a/compiler/rustc_lexer/src/unescape/tests.rs b/compiler/rustc_lexer/src/unescape/tests.rs
index 1f4dbb20f4e..fa61554afde 100644
--- a/compiler/rustc_lexer/src/unescape/tests.rs
+++ b/compiler/rustc_lexer/src/unescape/tests.rs
@@ -106,6 +106,10 @@ fn test_unescape_str_warn() {
         assert_eq!(unescaped, expected);
     }
 
+    // Check we can handle escaped newlines at the end of a file.
+    check("\\\n", &[]);
+    check("\\\n ", &[]);
+
     check(
         "\\\n \u{a0} x",
         &[
@@ -115,6 +119,7 @@ fn test_unescape_str_warn() {
             (6..7, Ok('x')),
         ],
     );
+    check("\\\n  \n  x", &[(0..7, Err(EscapeError::MultipleSkippedLinesWarning)), (7..8, Ok('x'))]);
 }
 
 #[test]