Skip to content

collapsible-else-if (PLR5501) --fix removes comments #13545

@sylvestre

Description

@sylvestre

Trying to reformat firefox code with ruff https://phabricator.services.mozilla.com/D223851#inline-1243679
we noticed a bug with collapsible-else-if - https://docs.astral.sh/ruff/rules/collapsible-else-if/#collapsible-else-if-plr5501

take this case:

def check_sign(value: int) -> None:
    if value > 0:
        print("Number is positive.")
    else:
	# my comment to explain this
	# but will be removed
        if value < 0:
            print("Number is negative.")
        else:
            print("Number is zero.")

Then, with ruff main (ec72e67)

cargo run -p ruff -- check --select PLR5501 foo.py --no-cache  --fix  
Found 1 error (1 fixed, 0 remaining).

will update the code to:

def check_sign(value: int) -> None:
    if value > 0:
        print("Number is positive.")
    elif value < 0:
        print("Number is negative.")
    else:
        print("Number is zero.")

which removed my comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions