Treat escaped newline as valid sequence (#11465)

## Summary

We weren't treating the escaped newline as a valid condition to trigger
the safer fix (add an extra backslash before each invalid escape
sequence).

Closes https://github.com/astral-sh/ruff/issues/11461.
This commit is contained in:
Charlie Marsh 2024-05-18 23:32:32 -04:00 committed by GitHub
parent 48b0660228
commit cfceb437a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 71 additions and 0 deletions

View file

@ -46,3 +46,15 @@ regex = '\\\_'
#: W605:1:7 #: W605:1:7
u'foo\ bar' u'foo\ bar'
#: W605:1:13
(
"foo \
bar \. baz"
)
#: W605:1:6
"foo \. bar \t"
#: W605:1:13
"foo \t bar \."

View file

@ -181,6 +181,7 @@ fn check(
// If we're at the end of line, skip. // If we're at the end of line, skip.
if matches!(next_char, '\n' | '\r') { if matches!(next_char, '\n' | '\r') {
contains_valid_escape_sequence = true;
continue; continue;
} }

View file

@ -145,6 +145,8 @@ W605_0.py:48:6: W605 [*] Invalid escape sequence: `\ `
47 | #: W605:1:7 47 | #: W605:1:7
48 | u'foo\ bar' 48 | u'foo\ bar'
| ^^ W605 | ^^ W605
49 |
50 | #: W605:1:13
| |
= help: Use a raw string literal = help: Use a raw string literal
@ -154,5 +156,61 @@ W605_0.py:48:6: W605 [*] Invalid escape sequence: `\ `
47 47 | #: W605:1:7 47 47 | #: W605:1:7
48 |-u'foo\ bar' 48 |-u'foo\ bar'
48 |+r'foo\ bar' 48 |+r'foo\ bar'
49 49 |
50 50 | #: W605:1:13
51 51 | (
W605_0.py:53:9: W605 [*] Invalid escape sequence: `\.`
|
51 | (
52 | "foo \
53 | bar \. baz"
| ^^ W605
54 | )
|
= help: Add backslash to escape sequence
Safe fix
50 50 | #: W605:1:13
51 51 | (
52 52 | "foo \
53 |- bar \. baz"
53 |+ bar \\. baz"
54 54 | )
55 55 |
56 56 | #: W605:1:6
W605_0.py:57:6: W605 [*] Invalid escape sequence: `\.`
|
56 | #: W605:1:6
57 | "foo \. bar \t"
| ^^ W605
58 |
59 | #: W605:1:13
|
= help: Add backslash to escape sequence
Safe fix
54 54 | )
55 55 |
56 56 | #: W605:1:6
57 |-"foo \. bar \t"
57 |+"foo \\. bar \t"
58 58 |
59 59 | #: W605:1:13
60 60 | "foo \t bar \."
W605_0.py:60:13: W605 [*] Invalid escape sequence: `\.`
|
59 | #: W605:1:13
60 | "foo \t bar \."
| ^^ W605
|
= help: Add backslash to escape sequence
Safe fix
57 57 | "foo \. bar \t"
58 58 |
59 59 | #: W605:1:13
60 |-"foo \t bar \."
60 |+"foo \t bar \\."