From fccbe56d23bd9fcc1ebb876e47e1e8bc1a02bb1b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 17 Nov 2024 22:58:12 -0500 Subject: [PATCH] Reverse order of `__contains__` arguments (#14424) ## Summary Closes https://github.com/astral-sh/ruff/issues/14423. --- .../fixtures/pylint/unnecessary_dunder_call.py | 2 ++ .../pylint/rules/unnecessary_dunder_call.rs | 2 +- ...sts__PLC2801_unnecessary_dunder_call.py.snap | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) 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 71fde30f5e..342f69609f 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 @@ -100,3 +100,5 @@ class Thing: blah = lambda: {"a": 1}.__delitem__("a") # OK blah = dict[{"a": 1}.__delitem__("a")] # OK + +"abc".__contains__("a") 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 73bd449aa8..b854407bfb 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 @@ -248,7 +248,7 @@ impl DunderReplacement { match dunder_method { "__add__" => Some(Self::Operator("+", "Use `+` operator")), "__and__" => Some(Self::Operator("&", "Use `&` operator")), - "__contains__" => Some(Self::Operator("in", "Use `in` operator")), + "__contains__" => Some(Self::ROperator("in", "Use `in` operator")), "__eq__" => Some(Self::Operator("==", "Use `==` operator")), "__floordiv__" => Some(Self::Operator("//", "Use `//` operator")), "__ge__" => Some(Self::Operator(">=", "Use `>=` operator")), 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 0938f72c86..40632facb3 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 @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs -snapshot_kind: text --- unnecessary_dunder_call.py:4:7: PLC2801 [*] Unnecessary dunder call to `__add__`. Use `+` operator. | @@ -1082,3 +1081,19 @@ unnecessary_dunder_call.py:92:16: PLC2801 Unnecessary dunder call to `__getattri 94 | def use_descriptor(self, item): | = help: Access attribute directly or use getattr built-in function + +unnecessary_dunder_call.py:104:1: PLC2801 [*] Unnecessary dunder call to `__contains__`. Use `in` operator. + | +102 | blah = dict[{"a": 1}.__delitem__("a")] # OK +103 | +104 | "abc".__contains__("a") + | ^^^^^^^^^^^^^^^^^^^^^^^ PLC2801 + | + = help: Use `in` operator + +ℹ Unsafe fix +101 101 | +102 102 | blah = dict[{"a": 1}.__delitem__("a")] # OK +103 103 | +104 |-"abc".__contains__("a") + 104 |+"a" in "abc"