Skip to content

Commit 440635c

Browse files
authored
[pycodestyle] Add fix safety section to W291 and W293 (astral-sh#18800)
Part of astral-sh#15584 This PR adds fix safety sections to `W291` and `W293` The unsafe caveat was added in astral-sh#10049 https://github.com/astral-sh/ruff/blob/10a1d9f01e899201c8d37d29071fe68752d09544/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs#L92 Code example demonstrating unsafety: ``` PS ~\Desktop\New_folder\ruff>Get-Content issue.py ``` ```py # W291 """ 1 """ # W293 """ """ ``` ``` PS ~\Desktop\New_folder\ruff>Get-Escaped-Content issue.py ``` ``` # W291\n"""\n1 \n"""\n\n# W293\n"""\n \n"""\r\n ``` ``` PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select W ``` ```snap issue.py:3:2: W291 Trailing whitespace | 1 | # W291 2 | """ 3 | 1 | ^ W291 4 | """ | = help: Remove trailing whitespace issue.py:8:1: W293 Blank line contains whitespace | 6 | # W293 7 | """ 8 | | ^ W293 9 | """ | = help: Remove whitespace from blank line Found 2 errors. No fixes available (2 hidden fixes can be enabled with the `--unsafe-fixes` option). ``` ## Test Plan <!-- How was it tested? --> N/A, no tests affected.
1 parent ce0a32a commit 440635c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use crate::{AlwaysFixableViolation, Applicability, Edit, Fix};
2626
/// spam(1)\n#
2727
/// ```
2828
///
29+
/// ## Fix Safety
30+
///
31+
/// This fix is marked unsafe if the whitespace is inside a multiline string,
32+
/// as removing it changes the string's content.
33+
///
2934
/// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations
3035
#[derive(ViolationMetadata)]
3136
pub(crate) struct TrailingWhitespace;
@@ -58,6 +63,11 @@ impl AlwaysFixableViolation for TrailingWhitespace {
5863
/// class Foo(object):\n\n bang = 12
5964
/// ```
6065
///
66+
/// ## Fix Safety
67+
///
68+
/// This fix is marked unsafe if the whitespace is inside a multiline string,
69+
/// as removing it changes the string's content.
70+
///
6171
/// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations
6272
#[derive(ViolationMetadata)]
6373
pub(crate) struct BlankLineWithWhitespace;

0 commit comments

Comments
 (0)