-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implements some of #14738, by adding support for 6 new patterns: ```py re.search("abc", s) is None # ⇒ "abc" not in s re.search("abc", s) is not None # ⇒ "abc" in s re.match("abc", s) is None # ⇒ not s.startswith("abc") re.match("abc", s) is not None # ⇒ s.startswith("abc") re.fullmatch("abc", s) is None # ⇒ s != "abc" re.fullmatch("abc", s) is not None # ⇒ s == "abc" ``` ## Test Plan ```shell cargo nextest run cargo insta review ``` And ran the fix on my startup's repo. ## Note One minor limitation here: ```py if not re.match('abc', s) is None: pass ``` will get fixed to this (technically correct, just not nice): ```py if not not s.startswith('abc'): pass ``` This seems fine given that Ruff has this covered: the initial code should be caught by [E714](https://docs.astral.sh/ruff/rules/not-is-test/) and the fixed code should be caught by [SIM208](https://docs.astral.sh/ruff/rules/double-negation/).
- Loading branch information
Showing
6 changed files
with
344 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
crates/ruff_linter/resources/test/fixtures/ruff/RUF055_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Patterns that don't just involve the call, but rather the parent expression""" | ||
import re | ||
|
||
s = "str" | ||
|
||
# this should be replaced with `"abc" not in s` | ||
re.search("abc", s) is None | ||
|
||
|
||
# this should be replaced with `"abc" in s` | ||
re.search("abc", s) is not None | ||
|
||
|
||
# this should be replaced with `not s.startswith("abc")` | ||
re.match("abc", s) is None | ||
|
||
|
||
# this should be replaced with `s.startswith("abc")` | ||
re.match("abc", s) is not None | ||
|
||
|
||
# this should be replaced with `s != "abc"` | ||
re.fullmatch("abc", s) is None | ||
|
||
|
||
# this should be replaced with `s == "abc"` | ||
re.fullmatch("abc", s) is not None | ||
|
||
|
||
# this should trigger an unsafe fix because of the presence of a comment within the | ||
# expression being replaced (which we'd lose) | ||
if ( | ||
re.fullmatch( | ||
"a really really really really long string", | ||
s, | ||
) | ||
# with a comment here | ||
is None | ||
): | ||
pass | ||
|
||
|
||
# this should trigger a safe fix (comments are preserved given they're outside the | ||
# expression) | ||
if ( # leading | ||
re.fullmatch( | ||
"a really really really really long string", | ||
s, | ||
) | ||
is None # trailing | ||
): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.