diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB156.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB156.py index 8b086f108d..032e72aa98 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB156.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB156.py @@ -27,8 +27,26 @@ _ = ( # with comment ).capitalize() +# example with augmented assignment +_ += "0123456789" + # OK _ = "1234567890" _ = "1234" _ = "12" in "12345670" + + +# No errors as the string is considered as a docstring +class C: + "01234567" + + +class C: + def method(self): + "01234567" + + +def function(): + """01234567""" + diff --git a/crates/ruff_linter/src/rules/refurb/rules/hardcoded_string_charset.rs b/crates/ruff_linter/src/rules/refurb/rules/hardcoded_string_charset.rs index c9583cdbdd..0c0d0a4a42 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/hardcoded_string_charset.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/hardcoded_string_charset.rs @@ -46,6 +46,11 @@ impl AlwaysFixableViolation for HardcodedStringCharset { /// FURB156 pub(crate) fn hardcoded_string_charset_literal(checker: &Checker, expr: &ExprStringLiteral) { + // if the string literal is a docstring, the rule is not applied + if checker.semantic().in_pep_257_docstring() { + return; + } + if let Some(charset) = check_charset_exact(expr.value.to_str().as_bytes()) { push_diagnostic(checker, expr.range, charset); } diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB156_FURB156.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB156_FURB156.py.snap index c11a677491..2041d7694f 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB156_FURB156.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB156_FURB156.py.snap @@ -331,4 +331,30 @@ FURB156.py:26:5: FURB156 [*] Use of hardcoded string charset 27 |+ string.digits 27 28 | # with comment 28 29 | ).capitalize() -29 30 | +29 30 | + +FURB156.py:31:6: FURB156 [*] Use of hardcoded string charset + | +30 | # example with augmented assignment +31 | _ += "0123456789" + | ^^^^^^^^^^^^ FURB156 +32 | +33 | # OK + | + = help: Replace hardcoded charset with `string.digits` + +ℹ Safe fix +1 1 | # Errors + 2 |+import string +2 3 | +3 4 | _ = "0123456789" +4 5 | _ = "01234567" +-------------------------------------------------------------------------------- +28 29 | ).capitalize() +29 30 | +30 31 | # example with augmented assignment +31 |-_ += "0123456789" + 32 |+_ += string.digits +32 33 | +33 34 | # OK +34 35 |