[ruff] Fix false positive for re.split with empty string pattern (RUF055)#23634
Merged
ntBre merged 2 commits intoastral-sh:mainfrom Mar 2, 2026
Merged
Conversation
…RUF055`) Closes astral-sh#23629 `re.split("", s)` succeeds and returns `['', 'a', 'b', 'c', '']`, but the suggested fix `s.split("")` raises `ValueError: empty separator`. This adds a guard to skip the diagnostic when the separator pattern is an empty string or bytes literal for `re.split` calls.
|
ntBre
reviewed
Mar 2, 2026
Contributor
ntBre
left a comment
There was a problem hiding this comment.
Looks good, thank you! I just had a small refactoring suggestion.
crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_expression.rs
Outdated
Show resolved
Hide resolved
Address review feedback from ntBre: - Add is_empty() method to Literal enum using direct .is_empty() on StringLiteralValue and BytesLiteralValue - Simplify the call site to a single-line condition
re.split with empty string pattern (RUF055)ruff] Fix false positive for re.split with empty string pattern (RUF055)
AlexWaygood
pushed a commit
that referenced
this pull request
Mar 2, 2026
…(`RUF055`) (#23634) ## Summary Fixes #23629. `re.split("", s)` is flagged by RUF055 and auto-fixed to `s.split("")`, but `str.split("")` raises `ValueError: empty separator` while `re.split("", s)` succeeds (returning `["", "a", "b", "c", ""]`). The same applies to bytes (`rb""`). This adds a guard to skip the diagnostic when the separator pattern is an empty string or bytes literal specifically for `re.split` calls. Other `re` functions (`sub`, `match`, `search`, `fullmatch`) are not affected — their `str` equivalents all handle empty strings equivalently. ## Test Plan Added test cases for empty string and bytes patterns in `RUF055_0.py` and `RUF055_3.py`. Verified that no diagnostics are emitted for these cases and all existing RUF055 snapshot tests continue to pass: ``` cargo test -p ruff_linter -- "preview_rules::rule_unnecessaryregularexpression" test result: ok. 4 passed; 0 failed; 0 ignored ```
carljm
added a commit
that referenced
this pull request
Mar 2, 2026
* main: (30 commits) [ty] Introduce `types::bool`, `types::context_manager` and `types::iteration` (#23681) [ty] Move `KnownInstanceType`, and related types, to a new `known_instance.rs` submodule (#23680) [ty] Add `invalid-enum-member-annotation` lint rule (#23648) [`ruff`] Fix false positive for `re.split` with empty string pattern (`RUF055`) (#23634) [ty] Move `UnionType` and `IntersectionType` to a new `types::set_theoretic` submodule (#23678) [ty] Add unbound type variable detection in annotations (#23641) [ty] Remove `specialize_constrained` from constraint set module (#23677) [ty] Add partial support and validation for `Unpack` when used with tuple types (#23651) Update prek dependencies (#23661) [ty] make `StaticClassLiteral::explicit_bases` converge better in cycles (#23601) Improvements to CLAUDE.md (#23633) [ty] Move subscript logic out of `builder.rs` (#23653) Update Artifact GitHub Actions dependencies (#23676) Update actions/attest-build-provenance to 4.1.0 (#23654) Update Rust crate clearscreen to v4.0.5 (#23664) fix renovate `actions/*-artifact` updates (#23675) Update Rust crate clap to v4.5.60 (#23663) Update Rust crate unicode-ident to v1.0.24 (#23668) Update Rust crate anyhow to v1.0.102 (#23662) Update Rust crate pyproject-toml to v0.13.7 (#23666) ...
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #23629.
re.split("", s)is flagged by RUF055 and auto-fixed tos.split(""), butstr.split("")raisesValueError: empty separatorwhilere.split("", s)succeeds (returning["", "a", "b", "c", ""]). The same applies to bytes (rb"").This adds a guard to skip the diagnostic when the separator pattern is an empty string or bytes literal specifically for
re.splitcalls. Otherrefunctions (sub,match,search,fullmatch) are not affected — theirstrequivalents all handle empty strings equivalently.Test Plan
Added test cases for empty string and bytes patterns in
RUF055_0.pyandRUF055_3.py. Verified that no diagnostics are emitted for these cases and all existing RUF055 snapshot tests continue to pass: