From 440635cbe6287c345b87780943fdc3172e8fc02f Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Thu, 19 Jun 2025 14:48:02 -0700 Subject: [PATCH] [`pycodestyle`] Add fix safety section to `W291` and `W293` (#18800) Part of #15584 This PR adds fix safety sections to `W291` and `W293` The unsafe caveat was added in #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 N/A, no tests affected. --- .../src/rules/pycodestyle/rules/trailing_whitespace.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 1da5f5ba17..1f2b8f94b5 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs @@ -26,6 +26,11 @@ use crate::{AlwaysFixableViolation, Applicability, Edit, Fix}; /// spam(1)\n# /// ``` /// +/// ## Fix Safety +/// +/// This fix is marked unsafe if the whitespace is inside a multiline string, +/// as removing it changes the string's content. +/// /// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations #[derive(ViolationMetadata)] pub(crate) struct TrailingWhitespace; @@ -58,6 +63,11 @@ impl AlwaysFixableViolation for TrailingWhitespace { /// class Foo(object):\n\n bang = 12 /// ``` /// +/// ## Fix Safety +/// +/// This fix is marked unsafe if the whitespace is inside a multiline string, +/// as removing it changes the string's content. +/// /// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations #[derive(ViolationMetadata)] pub(crate) struct BlankLineWithWhitespace;