Skip to content

Commit

Permalink
[ruff] Treat ) as a regex metacharacter (RUF043, RUF055) (#15318
Browse files Browse the repository at this point in the history
)

## Summary

Resolves #15316.

## Test Plan

`cargo nextest run` and `cargo insta test`.
  • Loading branch information
InSyncWithFoo authored Jan 7, 2025
1 parent 5567e7c commit e413956
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 209 deletions.
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF043.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def test_foo():
with pytest.raises(EscapedFollowedByUnescaped, match="foo\\.*bar"): ...
with pytest.raises(UnescapedFollowedByEscaped, match="foo.\\*bar"): ...

# https://github.com/astral-sh/ruff/issues/15316
with pytest.raises(ClosingParenthesis, match="foo)"): ...


## Metasequences

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def dashrepl(matchobj):
re.sub(r"abc\n", "", s) # this one could be fixed but is not currently
re.sub(r"abc|def", "", s)
re.sub(r"(a)bc", "", s)
re.sub(r"a)bc", "", s) # https://github.com/astral-sh/ruff/issues/15316

# and these should not be modified because they have extra arguments
re.sub("abc", "", s, flags=re.A)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ const fn escaped_char_is_regex_metasequence(c: char) -> bool {
const fn char_is_regex_metacharacter(c: char) -> bool {
matches!(
c,
'.' | '^' | '$' | '*' | '+' | '?' | '{' | '[' | '\\' | '|' | '('
'.' | '^' | '$' | '*' | '+' | '?' | '{' | '[' | '\\' | '|' | '(' | ')'
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(crate) fn unnecessary_regular_expression(checker: &mut Checker, call: &ExprC
let has_metacharacters = string_lit
.value
.to_str()
.contains(['.', '^', '$', '*', '+', '?', '{', '[', '\\', '|', '(']);
.contains(['.', '^', '$', '*', '+', '?', '{', '[', '\\', '|', '(', ')']);

if has_metacharacters {
return;
Expand Down
Loading

0 comments on commit e413956

Please sign in to comment.