Skip to content

Commit b89d61b

Browse files
[FURB156] Do not consider docstring(s) (#16391)
Co-authored-by: Micha Reiser <[email protected]>
1 parent 8c0eac2 commit b89d61b

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,26 @@
2727
# with comment
2828
).capitalize()
2929

30+
# example with augmented assignment
31+
_ += "0123456789"
32+
3033
# OK
3134

3235
_ = "1234567890"
3336
_ = "1234"
3437
_ = "12" in "12345670"
38+
39+
40+
# No errors as the string is considered as a docstring
41+
class C:
42+
"01234567"
43+
44+
45+
class C:
46+
def method(self):
47+
"01234567"
48+
49+
50+
def function():
51+
"""01234567"""
52+

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

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl AlwaysFixableViolation for HardcodedStringCharset {
4646

4747
/// FURB156
4848
pub(crate) fn hardcoded_string_charset_literal(checker: &Checker, expr: &ExprStringLiteral) {
49+
// if the string literal is a docstring, the rule is not applied
50+
if checker.semantic().in_pep_257_docstring() {
51+
return;
52+
}
53+
4954
if let Some(charset) = check_charset_exact(expr.value.to_str().as_bytes()) {
5055
push_diagnostic(checker, expr.range, charset);
5156
}

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,30 @@ FURB156.py:26:5: FURB156 [*] Use of hardcoded string charset
331331
27 |+ string.digits
332332
27 28 | # with comment
333333
28 29 | ).capitalize()
334-
29 30 |
334+
29 30 |
335+
336+
FURB156.py:31:6: FURB156 [*] Use of hardcoded string charset
337+
|
338+
30 | # example with augmented assignment
339+
31 | _ += "0123456789"
340+
| ^^^^^^^^^^^^ FURB156
341+
32 |
342+
33 | # OK
343+
|
344+
= help: Replace hardcoded charset with `string.digits`
345+
346+
Safe fix
347+
1 1 | # Errors
348+
2 |+import string
349+
2 3 |
350+
3 4 | _ = "0123456789"
351+
4 5 | _ = "01234567"
352+
--------------------------------------------------------------------------------
353+
28 29 | ).capitalize()
354+
29 30 |
355+
30 31 | # example with augmented assignment
356+
31 |-_ += "0123456789"
357+
32 |+_ += string.digits
358+
32 33 |
359+
33 34 | # OK
360+
34 35 |

0 commit comments

Comments
 (0)