Skip to content

[ruff] Fix false positive for re.split with empty string pattern (RUF055)#23634

Merged
ntBre merged 2 commits intoastral-sh:mainfrom
bxff:fix/ruf055-empty-split-false-positive
Mar 2, 2026
Merged

[ruff] Fix false positive for re.split with empty string pattern (RUF055)#23634
ntBre merged 2 commits intoastral-sh:mainfrom
bxff:fix/ruf055-empty-split-false-positive

Conversation

@bxff
Copy link
Contributor

@bxff bxff commented Feb 28, 2026

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

…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.
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 28, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added bug Something isn't working rule Implementing or modifying a lint rule preview Related to preview mode features labels Mar 2, 2026
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you! I just had a small refactoring suggestion.

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
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you!

@ntBre ntBre changed the title [ruff] Fix false positive for re.split with empty string pattern (RUF055) [ruff] Fix false positive for re.split with empty string pattern (RUF055) Mar 2, 2026
@ntBre ntBre merged commit e891d6c into astral-sh:main Mar 2, 2026
43 checks passed
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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RUF055] False positive for re.split with empty string pattern

3 participants