Skip to content

Commit 730e469

Browse files
authored
FURB167: change example to re.search with ^ anchor (#22984)
## Summary The example for [FURB167](https://docs.astral.sh/ruff/rules/regex-flag-alias/#regex-flag-alias-furb167) has `re.match` with an `^` anchor to match the start of the string: ```python if re.match("^hello", "hello world", re.I): ``` But `re.match` already implicitly matches the start of the string: https://docs.python.org/3/library/re.html#search-vs-match Let's change the example to `re.search` so the anchor isn't redundant. (The anchor's actually irrelevant to the example for this rule about long or short flag names.) (Aside: There's a discussion about adding `re.prefixmatch` and [soft] deprecating `re.match` because of the confusion around it: https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927, python/cpython#86519, python/cpython#31137.) ## Test Plan <!-- How was it tested? --> 1. Create feature branch 2. Push to my fork to run CI 3. Realise feature branches are disabled for forks in Ruff CI 4. Merge feature branch to my `main` 5. Push that 6. Be happy I did, because it failed because I missed something 7. Fixup, pushup 8. Passes [🎉](https://github.com/hugovk/ruff/actions/runs/21524112749)
1 parent e84608b commit 730e469

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

crates/ruff_linter/resources/test/fixtures/refurb/FURB167.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ def func():
22
import re
33

44
# OK
5-
if re.match("^hello", "hello world", re.IGNORECASE):
5+
if re.search("^hello", "hello world", re.IGNORECASE):
66
pass
77

88

99
def func():
1010
import re
1111

1212
# FURB167
13-
if re.match("^hello", "hello world", re.I):
13+
if re.search("^hello", "hello world", re.I):
1414
pass
1515

1616

1717
def func():
18-
from re import match, I
18+
from re import search, I
1919

2020
# FURB167
21-
if match("^hello", "hello world", I):
21+
if search("^hello", "hello world", I):
2222
pass

crates/ruff_linter/src/rules/refurb/rules/regex_flag_alias.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
2020
/// ```python
2121
/// import re
2222
///
23-
/// if re.match("^hello", "hello world", re.I):
23+
/// if re.search("^hello", "hello world", re.I):
2424
/// ...
2525
/// ```
2626
///
2727
/// Use instead:
2828
/// ```python
2929
/// import re
3030
///
31-
/// if re.match("^hello", "hello world", re.IGNORECASE):
31+
/// if re.search("^hello", "hello world", re.IGNORECASE):
3232
/// ...
3333
/// ```
3434
#[derive(ViolationMetadata)]

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB167_FURB167.py.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
source: crates/ruff_linter/src/rules/refurb/mod.rs
33
---
44
FURB167 [*] Use of regular expression alias `re.I`
5-
--> FURB167.py:13:42
5+
--> FURB167.py:13:43
66
|
77
12 | # FURB167
8-
13 | if re.match("^hello", "hello world", re.I):
9-
| ^^^^
8+
13 | if re.search("^hello", "hello world", re.I):
9+
| ^^^^
1010
14 | pass
1111
|
1212
help: Replace with `re.IGNORECASE`
1313
10 | import re
1414
11 |
1515
12 | # FURB167
16-
- if re.match("^hello", "hello world", re.I):
17-
13 + if re.match("^hello", "hello world", re.IGNORECASE):
16+
- if re.search("^hello", "hello world", re.I):
17+
13 + if re.search("^hello", "hello world", re.IGNORECASE):
1818
14 | pass
1919
15 |
2020
16 |
2121

2222
FURB167 [*] Use of regular expression alias `re.I`
23-
--> FURB167.py:21:39
23+
--> FURB167.py:21:40
2424
|
2525
20 | # FURB167
26-
21 | if match("^hello", "hello world", I):
27-
| ^
26+
21 | if search("^hello", "hello world", I):
27+
| ^
2828
22 | pass
2929
|
3030
help: Replace with `re.IGNORECASE`
@@ -33,9 +33,9 @@ help: Replace with `re.IGNORECASE`
3333
3 | import re
3434
4 |
3535
--------------------------------------------------------------------------------
36-
19 | from re import match, I
36+
19 | from re import search, I
3737
20 |
3838
21 | # FURB167
39-
- if match("^hello", "hello world", I):
40-
22 + if match("^hello", "hello world", re.IGNORECASE):
39+
- if search("^hello", "hello world", I):
40+
22 + if search("^hello", "hello world", re.IGNORECASE):
4141
23 | pass

0 commit comments

Comments
 (0)