diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/W293.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/W293.py index 3f0996a451dca..eed34461de471 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/W293.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/W293.py @@ -14,3 +14,6 @@ class Chassis(RobotModuleTemplate): " \ \ +'''blank line with whitespace + +inside a multiline string''' diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs index 42fb45ec64da4..fee7bc5ffbcb5 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs @@ -86,30 +86,33 @@ pub(crate) fn trailing_whitespace( .sum(); if whitespace_len > TextSize::from(0) { let range = TextRange::new(line.end() - whitespace_len, line.end()); - + // Removing trailing whitespace is not safe inside multiline strings. + let applicability = if indexer.multiline_ranges().contains_range(range) { + Applicability::Unsafe + } else { + Applicability::Safe + }; if range == line.range() { if settings.rules.enabled(Rule::BlankLineWithWhitespace) { let mut diagnostic = Diagnostic::new(BlankLineWithWhitespace, range); // Remove any preceding continuations, to avoid introducing a potential // syntax error. - diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(TextRange::new( - indexer - .preceded_by_continuations(line.start(), locator) - .unwrap_or(range.start()), - range.end(), - )))); + diagnostic.set_fix(Fix::applicable_edit( + Edit::range_deletion(TextRange::new( + indexer + .preceded_by_continuations(line.start(), locator) + .unwrap_or(range.start()), + range.end(), + )), + applicability, + )); return Some(diagnostic); } } else if settings.rules.enabled(Rule::TrailingWhitespace) { let mut diagnostic = Diagnostic::new(TrailingWhitespace, range); diagnostic.set_fix(Fix::applicable_edit( Edit::range_deletion(range), - // Removing trailing whitespace is not safe inside multiline strings. - if indexer.multiline_ranges().contains_range(range) { - Applicability::Unsafe - } else { - Applicability::Safe - }, + applicability, )); return Some(diagnostic); } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__W293_W293.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__W293_W293.py.snap index 5c949970f3119..a8f5782d43578 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__W293_W293.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__W293_W293.py.snap @@ -11,7 +11,7 @@ W293.py:4:1: W293 [*] Blank line contains whitespace | = help: Remove whitespace from blank line -ℹ Safe fix +ℹ Unsafe fix 1 1 | # See: https://github.com/astral-sh/ruff/issues/9323 2 2 | class Chassis(RobotModuleTemplate): 3 3 | """底盘信息推送控制 @@ -48,6 +48,7 @@ W293.py:16:1: W293 [*] Blank line contains whitespace 15 | \ 16 | | ^^^^ W293 +17 | '''blank line with whitespace | = help: Remove whitespace from blank line @@ -59,5 +60,25 @@ W293.py:16:1: W293 [*] Blank line contains whitespace 15 |- \ 16 |- 14 |+ " +17 15 | '''blank line with whitespace +18 16 | +19 17 | inside a multiline string''' + +W293.py:18:1: W293 [*] Blank line contains whitespace + | +17 | '''blank line with whitespace +18 | + | ^ W293 +19 | inside a multiline string''' + | + = help: Remove whitespace from blank line + +ℹ Unsafe fix +15 15 | \ +16 16 | +17 17 | '''blank line with whitespace +18 |- + 18 |+ +19 19 | inside a multiline string'''