ruff
62aa77df - Fix corner case involving terminal backslash after fixing `W293` (#5172)

Commit
2 years ago
Fix corner case involving terminal backslash after fixing `W293` (#5172) ## Summary Fixes #4404. Consider this file: ```python if True: x = 1; \ <space><space><space> ``` The current implementation of W293 removes the 3 spaces on line 2. This fix changes the file to: ```python if True: x = 1; \ ``` A file can't end in a `\`, according to Python's [lexical analysis](https://docs.python.org/3/reference/lexical_analysis.html), so subsequent iterations of the autofixer fail (the AST-based ones specifically, since they depend on a valid syntax tree and get re-parsed). This patch examines the line before the line checked in `W293`. If its first non-whitespace character is a `\`, the patch will extend the diagnostic's fix range to all whitespace up until the previous line's *second* non-whitespace character; that is, it deletes all spaces and potential `\`s up until the next non-whitespace character on the previous line. ## Test Plan Ran `cargo run -p ruff_cli -- ~/Downloads/aa.py --fix --select W293,D100 --no-cache` against the above file. This resulted in: ``` /Users/evan/Downloads/aa.py:1:1: D100 Missing docstring in public module Found 2 errors (1 fixed, 1 remaining). ``` The file's contents, after the fix: ```python if True: x = 1;<space> ``` The `\` was removed, leaving the terminal space. The space should be handled by `Rule::TrailingWhitespace`, not `BlankLineWithWhitespace`.
Parents
Loading