From cff9c13c426c2b6b03bfa046c2cb3030c05f1214 Mon Sep 17 00:00:00 2001 From: Chandra Kiran G <121796315+kiran-4444@users.noreply.github.com> Date: Tue, 21 Jan 2025 19:17:06 +0530 Subject: [PATCH] feat: Update RUF055 to do var == value (#15605) This commit fixes RUF055 rule to format `re.fullmatch(pattern, var)` to `var == pattern` instead of the current `pattern == var` behaviour. This is more idiomatic and easy to understand. ## Summary This changes the current formatting behaviour of `re.fullmatch(pattern, var)` to format it to `var == pattern` instead of `pattern == var`. ## Test Plan I used a code file locally to see the updated formatting behaviour. Fixes https://github.com/astral-sh/ruff/issues/14733 --- .../src/rules/ruff/rules/unnecessary_regular_expression.rs | 7 ++++++- ...r__rules__ruff__tests__preview__RUF055_RUF055_0.py.snap | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_expression.rs b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_expression.rs index ac99258457..fe263f1926 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_expression.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_expression.rs @@ -241,7 +241,12 @@ impl<'a> ReFunc<'a> { // pattern in string ReFuncKind::Search => Some(self.compare_expr(CmpOp::In)), // string == pattern - ReFuncKind::Fullmatch => Some(self.compare_expr(CmpOp::Eq)), + ReFuncKind::Fullmatch => Some(Expr::Compare(ExprCompare { + range: TextRange::default(), + left: Box::new(self.string.clone()), + ops: Box::new([CmpOp::Eq]), + comparators: Box::new([self.pattern.clone()]), + })), // string.split(pattern) ReFuncKind::Split => Some(self.method_expr("split", vec![self.pattern.clone()])), } diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF055_RUF055_0.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF055_RUF055_0.py.snap index fc63097a61..f778d0a023 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF055_RUF055_0.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF055_RUF055_0.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs +snapshot_kind: text --- RUF055_0.py:6:1: RUF055 [*] Plain string pattern passed to `re` function | @@ -68,14 +69,14 @@ RUF055_0.py:34:4: RUF055 [*] Plain string pattern passed to `re` function 35 | pass 36 | re.fullmatch("abc", s) # this should not be replaced | - = help: Replace with `"abc" == s` + = help: Replace with `s == "abc"` ℹ Safe fix 31 31 | re.search("abc", s) # this should not be replaced 32 32 | 33 33 | # this should be replaced with "abc" == s 34 |-if re.fullmatch("abc", s): - 34 |+if "abc" == s: + 34 |+if s == "abc": 35 35 | pass 36 36 | re.fullmatch("abc", s) # this should not be replaced 37 37 |