-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ruff] Support byte strings (RUF055)
#18926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1d20b9
Implement RUF055 rule for unnecessary regular expressions
danparizher 37d950c
apply feedback
danparizher e19d1f0
Update crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_ex…
danparizher 8e03ab3
Update crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_ex…
danparizher 46c09b2
apply feedback
danparizher fd2ad42
remove additional const
danparizher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
crates/ruff_linter/resources/test/fixtures/ruff/RUF055_3.py
This file contains hidden or 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,24 @@ | ||
| import re | ||
|
|
||
| b_src = b"abc" | ||
|
|
||
| # Should be replaced with `b_src.replace(rb"x", b"y")` | ||
| re.sub(rb"x", b"y", b_src) | ||
|
|
||
| # Should be replaced with `b_src.startswith(rb"abc")` | ||
| if re.match(rb"abc", b_src): | ||
| pass | ||
|
|
||
| # Should be replaced with `rb"x" in b_src` | ||
| if re.search(rb"x", b_src): | ||
| pass | ||
|
|
||
| # Should be replaced with `b_src.split(rb"abc")` | ||
| re.split(rb"abc", b_src) | ||
|
|
||
| # Patterns containing metacharacters should NOT be replaced | ||
| re.sub(rb"ab[c]", b"", b_src) | ||
| re.match(rb"ab[c]", b_src) | ||
| re.search(rb"ab[c]", b_src) | ||
| re.fullmatch(rb"ab[c]", b_src) | ||
| re.split(rb"ab[c]", b_src) |
This file contains hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
...rc/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF055_RUF055_3.py.snap
This file contains hidden or 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,80 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
| --- | ||
| RUF055_3.py:6:1: RUF055 [*] Plain string pattern passed to `re` function | ||
| | | ||
| 5 | # Should be replaced with `b_src.replace(rb"x", b"y")` | ||
| 6 | re.sub(rb"x", b"y", b_src) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF055 | ||
| 7 | | ||
| 8 | # Should be replaced with `b_src.startswith(rb"abc")` | ||
| | | ||
| = help: Replace with `b_src.replace(rb"x", b"y")` | ||
|
|
||
| ℹ Safe fix | ||
| 3 3 | b_src = b"abc" | ||
| 4 4 | | ||
| 5 5 | # Should be replaced with `b_src.replace(rb"x", b"y")` | ||
| 6 |-re.sub(rb"x", b"y", b_src) | ||
| 6 |+b_src.replace(rb"x", b"y") | ||
| 7 7 | | ||
| 8 8 | # Should be replaced with `b_src.startswith(rb"abc")` | ||
| 9 9 | if re.match(rb"abc", b_src): | ||
|
|
||
| RUF055_3.py:9:4: RUF055 [*] Plain string pattern passed to `re` function | ||
| | | ||
| 8 | # Should be replaced with `b_src.startswith(rb"abc")` | ||
| 9 | if re.match(rb"abc", b_src): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ RUF055 | ||
| 10 | pass | ||
| | | ||
| = help: Replace with `b_src.startswith(rb"abc")` | ||
|
|
||
| ℹ Safe fix | ||
| 6 6 | re.sub(rb"x", b"y", b_src) | ||
| 7 7 | | ||
| 8 8 | # Should be replaced with `b_src.startswith(rb"abc")` | ||
| 9 |-if re.match(rb"abc", b_src): | ||
| 9 |+if b_src.startswith(rb"abc"): | ||
| 10 10 | pass | ||
| 11 11 | | ||
| 12 12 | # Should be replaced with `rb"x" in b_src` | ||
|
|
||
| RUF055_3.py:13:4: RUF055 [*] Plain string pattern passed to `re` function | ||
| | | ||
| 12 | # Should be replaced with `rb"x" in b_src` | ||
| 13 | if re.search(rb"x", b_src): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^ RUF055 | ||
| 14 | pass | ||
| | | ||
| = help: Replace with `rb"x" in b_src` | ||
|
|
||
| ℹ Safe fix | ||
| 10 10 | pass | ||
| 11 11 | | ||
| 12 12 | # Should be replaced with `rb"x" in b_src` | ||
| 13 |-if re.search(rb"x", b_src): | ||
| 13 |+if rb"x" in b_src: | ||
| 14 14 | pass | ||
| 15 15 | | ||
| 16 16 | # Should be replaced with `b_src.split(rb"abc")` | ||
|
|
||
| RUF055_3.py:17:1: RUF055 [*] Plain string pattern passed to `re` function | ||
| | | ||
| 16 | # Should be replaced with `b_src.split(rb"abc")` | ||
| 17 | re.split(rb"abc", b_src) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ RUF055 | ||
| 18 | | ||
| 19 | # Patterns containing metacharacters should NOT be replaced | ||
| | | ||
| = help: Replace with `b_src.split(rb"abc")` | ||
|
|
||
| ℹ Safe fix | ||
| 14 14 | pass | ||
| 15 15 | | ||
| 16 16 | # Should be replaced with `b_src.split(rb"abc")` | ||
| 17 |-re.split(rb"abc", b_src) | ||
| 17 |+b_src.split(rb"abc") | ||
| 18 18 | | ||
| 19 19 | # Patterns containing metacharacters should NOT be replaced | ||
| 20 20 | re.sub(rb"ab[c]", b"", b_src) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.