[pylint] Supress PLE2510/2512/2513/2514/2515 autofix if the text contains an odd number of backslashes (#18856)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Victor Hugo Gomes 2025-06-23 05:11:51 -03:00 committed by GitHub
parent 96660d93ca
commit 659ecba477
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 13 additions and 8 deletions

View file

@ -193,6 +193,15 @@ pub(crate) fn invalid_string_characters(context: &LintContext, token: &Token, lo
let location = token.start() + TextSize::try_from(column).unwrap();
let c = match_.chars().next().unwrap();
let range = TextRange::at(location, c.text_len());
let is_escaped = &text[..column]
.chars()
.rev()
.take_while(|c| *c == '\\')
.count()
% 2
== 1;
let (replacement, diagnostic) = match c {
'\x08' => (
"\\b",
@ -223,7 +232,7 @@ pub(crate) fn invalid_string_characters(context: &LintContext, token: &Token, lo
continue;
};
if !token.unwrap_string_flags().is_raw_string() {
if !token.unwrap_string_flags().is_raw_string() && !is_escaped {
let edit = Edit::range_replacement(replacement.to_string(), range);
diagnostic.set_fix(Fix::safe_edit(edit));
}

View file

@ -232,14 +232,10 @@ if True:
#[test]
fn quick_test() {
let source = r#"
def main() -> None:
if True:
some_very_long_variable_name_abcdefghijk = Foo()
some_very_long_variable_name_abcdefghijk = some_very_long_variable_name_abcdefghijk[
some_very_long_variable_name_abcdefghijk.some_very_long_attribute_name
== "This is a very long string abcdefghijk"
]
def hello(): ...
@lambda _, /: _
class A: ...
"#;
let source_type = PySourceType::Python;