Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF055_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ def dashrepl(matchobj):
re.sub(r'abc', "", s)
re.sub(r"""abc""", "", s)
re.sub(r'''abc''', "", s)

# Empty pattern: re.split("", s) should not be flagged because
# str.split("") raises ValueError while re.split("", s) succeeds
re.split("", s)
re.split(r"", s)
5 changes: 4 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/ruff/RUF055_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
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)
re.split(rb"ab[c]", b_src)

# Empty pattern: re.split(rb"", b_src) should not be flagged
re.split(rb"", b_src)
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ pub(crate) fn unnecessary_regular_expression(checker: &Checker, call: &ExprCall)
return;
}

// `str.split("")` raises `ValueError: empty separator` while `re.split("", s)` succeeds,
// so skip the diagnostic for `re.split` with an empty pattern.
if matches!(re_func.kind, ReFuncKind::Split) && literal.is_empty() {
return;
}

// Now we know the pattern is a string literal with no metacharacters, so
// we can proceed with the str method replacement.
let new_expr = re_func.replacement();
Expand Down Expand Up @@ -362,6 +368,15 @@ enum Literal<'a> {
Bytes(&'a ExprBytesLiteral),
}

impl Literal<'_> {
fn is_empty(&self) -> bool {
match self {
Literal::Str(str_lit) => str_lit.value.is_empty(),
Literal::Bytes(bytes_lit) => bytes_lit.value.is_empty(),
}
}
}

/// Try to resolve `name` to either a string or bytes literal in `semantic`.
fn resolve_literal<'a>(name: &'a Expr, semantic: &'a SemanticModel) -> Option<Literal<'a>> {
if let Some(str_lit) = resolve_string_literal(name, semantic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ help: Replace with `s.replace(r'abc', "")`
98 + s.replace(r'abc', "")
99 | re.sub(r"""abc""", "", s)
100 | re.sub(r'''abc''', "", s)
101 |

RUF055 [*] Plain string pattern passed to `re` function
--> RUF055_0.py:99:1
Expand All @@ -260,6 +261,8 @@ help: Replace with `s.replace(r"""abc""", "")`
- re.sub(r"""abc""", "", s)
99 + s.replace(r"""abc""", "")
100 | re.sub(r'''abc''', "", s)
101 |
102 | # Empty pattern: re.split("", s) should not be flagged because

RUF055 [*] Plain string pattern passed to `re` function
--> RUF055_0.py:100:1
Expand All @@ -268,10 +271,15 @@ RUF055 [*] Plain string pattern passed to `re` function
99 | re.sub(r"""abc""", "", s)
100 | re.sub(r'''abc''', "", s)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
101 |
102 | # Empty pattern: re.split("", s) should not be flagged because
|
help: Replace with `s.replace(r'''abc''', "")`
97 | # these double as tests for preserving raw string quoting style
98 | re.sub(r'abc', "", s)
99 | re.sub(r"""abc""", "", s)
- re.sub(r'''abc''', "", s)
100 + s.replace(r'''abc''', "")
101 |
102 | # Empty pattern: re.split("", s) should not be flagged because
103 | # str.split("") raises ValueError while re.split("", s) succeeds