Skip to content

W291: ruff --fix removes trailing whitespace in multiline string #8037

@Jasha10

Description

@Jasha10

When W291 (trailing-whitespace) is enabled, ruff warns about trailing whitespace in multiline strings.
When --fix is passed, ruff modifies the multiline string. This could result in a behavior change of the python code.

Repro:

# repro.py
string = """
this line has trailing whitespace 
"""
$ ruff repro.py --isolated --select W291
repro.py:3:34: W291 [*] Trailing whitespace
Found 1 error.
[*] 1 potentially fixable with the --fix option.
$ ruff repro.py --isolated --select W291 --fix  # This modifies the multiline string!
Found 1 error (1 fixed, 0 remaining).
$ ruff --version
ruff 0.0.278

I noticed that the implementation trailing_whitespace categorizes the --fix action as Fix::safe_edit. I feel that modifying a multiline string is not safe; it can change the behavior of my python code. The doc for Applicability::Safe say:

    /// The fix is safe and can always be applied.
    /// The fix is definitely what the user intended, or it maintains the exact meaning of the code.

I feel that this does not apply to W291 as implemented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions