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
This commit is contained in:
Chandra Kiran G 2025-01-21 19:17:06 +05:30 committed by GitHub
parent 4656e3c90f
commit cff9c13c42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View File

@ -241,7 +241,12 @@ impl<'a> ReFunc<'a> {
// pattern in string // pattern in string
ReFuncKind::Search => Some(self.compare_expr(CmpOp::In)), ReFuncKind::Search => Some(self.compare_expr(CmpOp::In)),
// string == pattern // 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) // string.split(pattern)
ReFuncKind::Split => Some(self.method_expr("split", vec![self.pattern.clone()])), ReFuncKind::Split => Some(self.method_expr("split", vec![self.pattern.clone()])),
} }

View File

@ -1,5 +1,6 @@
--- ---
source: crates/ruff_linter/src/rules/ruff/mod.rs 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 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 35 | pass
36 | re.fullmatch("abc", s) # this should not be replaced 36 | re.fullmatch("abc", s) # this should not be replaced
| |
= help: Replace with `"abc" == s` = help: Replace with `s == "abc"`
Safe fix Safe fix
31 31 | re.search("abc", s) # this should not be replaced 31 31 | re.search("abc", s) # this should not be replaced
32 32 | 32 32 |
33 33 | # this should be replaced with "abc" == s 33 33 | # this should be replaced with "abc" == s
34 |-if re.fullmatch("abc", s): 34 |-if re.fullmatch("abc", s):
34 |+if "abc" == s: 34 |+if s == "abc":
35 35 | pass 35 35 | pass
36 36 | re.fullmatch("abc", s) # this should not be replaced 36 36 | re.fullmatch("abc", s) # this should not be replaced
37 37 | 37 37 |