Skip to content

Commit ddf0e27

Browse files
committed
Spruce up docs for slice-to-remove-prefix-or-suffix (FURB188)
1 parent a876090 commit ddf0e27

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@ use crate::Locator;
88
use crate::{checkers::ast::Checker, settings::types::PythonVersion};
99

1010
/// ## What it does
11-
/// Checks for the removal of a prefix or suffix from a string by assigning
12-
/// the string to a slice after checking `.startswith()` or `.endswith()`, respectively.
11+
/// Checks for code that could be written more idiomatically using
12+
/// [`str.removeprefix()`](https://docs.python.org/3/library/stdtypes.html#str.removeprefix)
13+
/// or [`str.removesuffix()`](https://docs.python.org/3/library/stdtypes.html#str.removesuffix).
14+
///
15+
/// Specifically, the rule flags code that conditionally removes a prefix or suffix
16+
/// using a slice operation following an `if` test that uses `str.startswith()` or `str.endswith()`.
17+
///
18+
/// The rule is only applied if your project targets Python 3.9 or later.
1319
///
1420
/// ## Why is this bad?
15-
/// The methods [`str.removeprefix`](https://docs.python.org/3/library/stdtypes.html#str.removeprefix)
16-
/// and [`str.removesuffix`](https://docs.python.org/3/library/stdtypes.html#str.removesuffix),
17-
/// introduced in Python 3.9, have the same behavior
18-
/// and are more readable and efficient.
21+
/// The methods [`str.removeprefix()`](https://docs.python.org/3/library/stdtypes.html#str.removeprefix)
22+
/// and [`str.removesuffix()`](https://docs.python.org/3/library/stdtypes.html#str.removesuffix),
23+
/// introduced in Python 3.9, have the same behavior while being more readable and efficient.
1924
///
2025
/// ## Example
2126
/// ```python
22-
/// filename[:-4] if filename.endswith(".txt") else filename
23-
/// ```
27+
/// def example(filename: str, text: str):
28+
/// filename = filename[:-4] if filename.endswith(".txt") else filename
2429
///
25-
/// ```python
26-
/// if text.startswith("pre"):
27-
/// text = text[3:]
30+
/// if text.startswith("pre"):
31+
/// text = text[3:]
2832
/// ```
2933
///
3034
/// Use instead:
3135
/// ```python
32-
/// filename = filename.removesuffix(".txt")
33-
/// ```
34-
///
35-
/// ```python
36-
/// text = text.removeprefix("pre")
36+
/// def example(filename: str, text: str):
37+
/// filename = filename.removesuffix(".txt")
38+
/// text = text.removeprefix("pre")
3739
/// ```
3840
#[derive(ViolationMetadata)]
3941
pub(crate) struct SliceToRemovePrefixOrSuffix {
@@ -46,10 +48,10 @@ impl AlwaysFixableViolation for SliceToRemovePrefixOrSuffix {
4648
fn message(&self) -> String {
4749
match self.affix_kind {
4850
AffixKind::StartsWith => {
49-
"Prefer `removeprefix` over conditionally replacing with slice.".to_string()
51+
"Prefer `str.removeprefix()` over conditionally replacing with slice.".to_string()
5052
}
5153
AffixKind::EndsWith => {
52-
"Prefer `removesuffix` over conditionally replacing with slice.".to_string()
54+
"Prefer `str.removesuffix()` over conditionally replacing with slice.".to_string()
5355
}
5456
}
5557
}

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/refurb/mod.rs
3-
snapshot_kind: text
43
---
5-
FURB188.py:7:5: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
4+
FURB188.py:7:5: FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
65
|
76
6 | def remove_extension_via_slice(filename: str) -> str:
87
7 | if filename.endswith(".txt"):
@@ -25,7 +24,7 @@ FURB188.py:7:5: FURB188 [*] Prefer `removesuffix` over conditionally replacing w
2524
10 9 | return filename
2625
11 10 |
2726

28-
FURB188.py:14:5: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
27+
FURB188.py:14:5: FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
2928
|
3029
13 | def remove_extension_via_slice_len(filename: str, extension: str) -> str:
3130
14 | if filename.endswith(extension):
@@ -48,7 +47,7 @@ FURB188.py:14:5: FURB188 [*] Prefer `removesuffix` over conditionally replacing
4847
17 16 | return filename
4948
18 17 |
5049

51-
FURB188.py:21:12: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
50+
FURB188.py:21:12: FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
5251
|
5352
20 | def remove_extension_via_ternary(filename: str) -> str:
5453
21 | return filename[:-4] if filename.endswith(".txt") else filename
@@ -66,7 +65,7 @@ FURB188.py:21:12: FURB188 [*] Prefer `removesuffix` over conditionally replacing
6665
23 23 |
6766
24 24 | def remove_extension_via_ternary_with_len(filename: str, extension: str) -> str:
6867

69-
FURB188.py:25:12: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
68+
FURB188.py:25:12: FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
7069
|
7170
24 | def remove_extension_via_ternary_with_len(filename: str, extension: str) -> str:
7271
25 | return filename[:-len(extension)] if filename.endswith(extension) else filename
@@ -84,7 +83,7 @@ FURB188.py:25:12: FURB188 [*] Prefer `removesuffix` over conditionally replacing
8483
27 27 |
8584
28 28 | def remove_prefix(filename: str) -> str:
8685

87-
FURB188.py:29:12: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
86+
FURB188.py:29:12: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
8887
|
8988
28 | def remove_prefix(filename: str) -> str:
9089
29 | return filename[4:] if filename.startswith("abc-") else filename
@@ -102,7 +101,7 @@ FURB188.py:29:12: FURB188 [*] Prefer `removeprefix` over conditionally replacing
102101
31 31 |
103102
32 32 | def remove_prefix_via_len(filename: str, prefix: str) -> str:
104103

105-
FURB188.py:33:12: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
104+
FURB188.py:33:12: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
106105
|
107106
32 | def remove_prefix_via_len(filename: str, prefix: str) -> str:
108107
33 | return filename[len(prefix):] if filename.startswith(prefix) else filename
@@ -120,7 +119,7 @@ FURB188.py:33:12: FURB188 [*] Prefer `removeprefix` over conditionally replacing
120119
35 35 |
121120
36 36 | # these should not
122121

123-
FURB188.py:146:9: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
122+
FURB188.py:146:9: FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
124123
|
125124
144 | SUFFIX = "suffix"
126125
145 |
@@ -141,7 +140,7 @@ FURB188.py:146:9: FURB188 [*] Prefer `removesuffix` over conditionally replacing
141140
148 148 | def remove_prefix_comparable_literal_expr() -> None:
142141
149 149 | return ("abc" "def")[3:] if ("abc" "def").startswith("abc") else "abc" "def"
143142

144-
FURB188.py:149:12: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
143+
FURB188.py:149:12: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
145144
|
146145
148 | def remove_prefix_comparable_literal_expr() -> None:
147146
149 | return ("abc" "def")[3:] if ("abc" "def").startswith("abc") else "abc" "def"
@@ -161,7 +160,7 @@ FURB188.py:149:12: FURB188 [*] Prefer `removeprefix` over conditionally replacin
161160
151 151 | def shadow_builtins(filename: str, extension: str) -> None:
162161
152 152 | from builtins import len as builtins_len
163162

164-
FURB188.py:154:12: FURB188 [*] Prefer `removesuffix` over conditionally replacing with slice.
163+
FURB188.py:154:12: FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
165164
|
166165
152 | from builtins import len as builtins_len
167166
153 |
@@ -182,7 +181,7 @@ FURB188.py:154:12: FURB188 [*] Prefer `removesuffix` over conditionally replacin
182181
156 156 | def okay_steps():
183182
157 157 | text = "!x!y!z"
184183

185-
FURB188.py:158:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
184+
FURB188.py:158:5: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
186185
|
187186
156 | def okay_steps():
188187
157 | text = "!x!y!z"
@@ -206,7 +205,7 @@ FURB188.py:158:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing
206205
161 160 | text = text[1::True]
207206
162 161 | if text.startswith("!"):
208207

209-
FURB188.py:160:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
208+
FURB188.py:160:5: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
210209
|
211210
158 | if text.startswith("!"):
212211
159 | text = text[1::1]
@@ -230,7 +229,7 @@ FURB188.py:160:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing
230229
163 162 | text = text[1::None]
231230
164 163 | print(text)
232231

233-
FURB188.py:162:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
232+
FURB188.py:162:5: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
234233
|
235234
160 | if text.startswith("!"):
236235
161 | text = text[1::True]
@@ -253,7 +252,7 @@ FURB188.py:162:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing
253252
165 164 |
254253
166 165 |
255254

256-
FURB188.py:183:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
255+
FURB188.py:183:5: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
257256
|
258257
181 | # with fix `text = text.removeprefix("ř")`
259258
182 | text = "řetězec"
@@ -275,7 +274,7 @@ FURB188.py:183:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing
275274
186 185 |
276275
187 186 | def handle_surrogates():
277276

278-
FURB188.py:190:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
277+
FURB188.py:190:5: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
279278
|
280279
188 | # should be linted
281280
189 | text = "\ud800\udc00heythere"
@@ -299,7 +298,7 @@ FURB188.py:190:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing
299298
193 192 | if text.startswith("\U00010000"):
300299
194 193 | text = text[1:]
301300

302-
FURB188.py:193:5: FURB188 [*] Prefer `removeprefix` over conditionally replacing with slice.
301+
FURB188.py:193:5: FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
303302
|
304303
191 | text = text[2:]
305304
192 | text = "\U00010000heythere"

0 commit comments

Comments
 (0)