From f4c6ff3f68ace5160c6b7bbdd734bec1885541a9 Mon Sep 17 00:00:00 2001 From: Victor Hugo Gomes Date: Mon, 23 Jun 2025 11:15:53 -0300 Subject: [PATCH] [`pylint`] Fix `PLC2801` autofix creating a syntax error (#18857) ## Summary This PR fixes `PLC2801` autofix creating a syntax error due to lack of padding if it is directly after a keyword. Fixes https://github.com/astral-sh/ruff/issues/18813 ## Test Plan Add regression test --- .../pylint/unnecessary_dunder_call.py | 3 +++ .../pylint/rules/unnecessary_dunder_call.rs | 3 ++- ...s__PLC2801_unnecessary_dunder_call.py.snap | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_dunder_call.py b/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_dunder_call.py index 2f46174fd3..8bdb4d6605 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_dunder_call.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/unnecessary_dunder_call.py @@ -129,3 +129,6 @@ blah = dict[{"a": 1}.__delitem__("a")] # OK # https://github.com/astral-sh/ruff/issues/14597 assert "abc".__str__() == "abc" + +# https://github.com/astral-sh/ruff/issues/18813 +three = 1 if 1 else(3.0).__str__() diff --git a/crates/ruff_linter/src/rules/pylint/rules/unnecessary_dunder_call.rs b/crates/ruff_linter/src/rules/pylint/rules/unnecessary_dunder_call.rs index a5b9c61aa9..62f69fad7b 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/unnecessary_dunder_call.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/unnecessary_dunder_call.rs @@ -4,6 +4,7 @@ use ruff_python_semantic::SemanticModel; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; +use crate::fix::edits; use crate::rules::pylint::helpers::is_known_dunder_method; use crate::{Edit, Fix, FixAvailability, Violation}; use ruff_python_ast::PythonVersion; @@ -232,7 +233,7 @@ pub(crate) fn unnecessary_dunder_call(checker: &Checker, call: &ast::ExprCall) { } diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( - fixed, + edits::pad(fixed, call.range(), checker.locator()), call.range(), ))); } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2801_unnecessary_dunder_call.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2801_unnecessary_dunder_call.py.snap index 4d897e73d9..971cc51c39 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2801_unnecessary_dunder_call.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2801_unnecessary_dunder_call.py.snap @@ -1193,6 +1193,8 @@ unnecessary_dunder_call.py:131:8: PLC2801 [*] Unnecessary dunder call to `__str_ 130 | # https://github.com/astral-sh/ruff/issues/14597 131 | assert "abc".__str__() == "abc" | ^^^^^^^^^^^^^^^ PLC2801 +132 | +133 | # https://github.com/astral-sh/ruff/issues/18813 | = help: Use `str()` builtin @@ -1202,3 +1204,21 @@ unnecessary_dunder_call.py:131:8: PLC2801 [*] Unnecessary dunder call to `__str_ 130 130 | # https://github.com/astral-sh/ruff/issues/14597 131 |-assert "abc".__str__() == "abc" 131 |+assert str("abc") == "abc" +132 132 | +133 133 | # https://github.com/astral-sh/ruff/issues/18813 +134 134 | three = 1 if 1 else(3.0).__str__() + +unnecessary_dunder_call.py:134:20: PLC2801 [*] Unnecessary dunder call to `__str__`. Use `str()` builtin. + | +133 | # https://github.com/astral-sh/ruff/issues/18813 +134 | three = 1 if 1 else(3.0).__str__() + | ^^^^^^^^^^^^^^^ PLC2801 + | + = help: Use `str()` builtin + +ℹ Unsafe fix +131 131 | assert "abc".__str__() == "abc" +132 132 | +133 133 | # https://github.com/astral-sh/ruff/issues/18813 +134 |-three = 1 if 1 else(3.0).__str__() + 134 |+three = 1 if 1 else str(3.0)