Skip to content

Commit d7e204f

Browse files
authored
Merge pull request #259 from dtolnay/lit
Require newline after backslash carriage return
2 parents b6b5a98 + 638a133 commit d7e204f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/parse.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ fn cooked_string(input: Cursor) -> Result<Cursor, LexError> {
349349
break;
350350
}
351351
}
352-
Some((_, '\n')) | Some((_, '\r')) => {
352+
Some((_, ch @ '\n')) | Some((_, ch @ '\r')) => {
353+
if ch == '\r' && chars.next().map_or(true, |(_, ch)| ch != '\n') {
354+
return Err(LexError);
355+
}
353356
while let Some(&(_, ch)) = chars.peek() {
354357
if ch.is_whitespace() {
355358
chars.next();
@@ -399,7 +402,10 @@ fn cooked_byte_string(mut input: Cursor) -> Result<Cursor, LexError> {
399402
}
400403
Some((_, b'n')) | Some((_, b'r')) | Some((_, b't')) | Some((_, b'\\'))
401404
| Some((_, b'0')) | Some((_, b'\'')) | Some((_, b'"')) => {}
402-
Some((newline, b'\n')) | Some((newline, b'\r')) => {
405+
Some((newline, b @ b'\n')) | Some((newline, b @ b'\r')) => {
406+
if b == b'\r' && bytes.next().map_or(true, |(_, b)| b != b'\n') {
407+
return Err(LexError);
408+
}
403409
let rest = input.advance(newline + 1);
404410
for (offset, ch) in rest.char_indices() {
405411
if !ch.is_whitespace() {

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ fn fail() {
197197
fail("\"\\u{}\""); // empty
198198
fail("b\"\r\""); // bare carriage return in byte string
199199
fail("r\"\r\""); // bare carriage return in raw string
200+
fail("\"\\\r \""); // backslash carriage return
200201
}
201202

202203
#[cfg(span_locations)]

0 commit comments

Comments
 (0)