[`pylint`] Fix unicode handling in `PLE2515` (#3898)

This commit is contained in:
konstin 2023-04-06 19:54:52 +02:00 committed by GitHub
parent 34e9786a41
commit cae5503e34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 4 deletions

View File

@ -184,16 +184,20 @@ pub fn invalid_string_characters(
let text = locator.slice(Range::new(start, end)); let text = locator.slice(Range::new(start, end));
for (row, line) in UniversalNewlineIterator::from(text).enumerate() { for (row, line) in UniversalNewlineIterator::from(text).enumerate() {
for (column, match_) in line.match_indices(&['\x08', '\x1A', '\x1B', '\0', '\u{200b}']) { let mut char_offset = 0;
let (replacement, rule): (&str, DiagnosticKind) = match match_.chars().next().unwrap() { for char in line.chars() {
let (replacement, rule): (&str, DiagnosticKind) = match char {
'\x08' => ("\\b", InvalidCharacterBackspace.into()), '\x08' => ("\\b", InvalidCharacterBackspace.into()),
'\x1A' => ("\\x1A", InvalidCharacterSub.into()), '\x1A' => ("\\x1A", InvalidCharacterSub.into()),
'\x1B' => ("\\x1B", InvalidCharacterEsc.into()), '\x1B' => ("\\x1B", InvalidCharacterEsc.into()),
'\0' => ("\\0", InvalidCharacterNul.into()), '\0' => ("\\0", InvalidCharacterNul.into()),
'\u{200b}' => ("\\u200b", InvalidCharacterZeroWidthSpace.into()), '\u{200b}' => ("\\u200b", InvalidCharacterZeroWidthSpace.into()),
_ => unreachable!(), _ => {
char_offset += 1;
continue;
}
}; };
let location = helpers::to_absolute(Location::new(row + 1, column), start); let location = helpers::to_absolute(Location::new(row + 1, char_offset), start);
let end_location = Location::new(location.row(), location.column() + 1); let end_location = Location::new(location.row(), location.column() + 1);
let mut diagnostic = Diagnostic::new(rule, Range::new(location, end_location)); let mut diagnostic = Diagnostic::new(rule, Range::new(location, end_location));
if autofix { if autofix {
@ -204,6 +208,7 @@ pub fn invalid_string_characters(
)); ));
} }
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
char_offset += 1;
} }
} }

View File

@ -23,4 +23,67 @@ expression: diagnostics
row: 34 row: 34
column: 13 column: 13
parent: ~ parent: ~
- kind:
name: InvalidCharacterZeroWidthSpace
body: "Invalid unescaped character zero-width-space, use \"\\u200B\" instead"
suggestion: Replace with escape sequence
fixable: true
location:
row: 38
column: 35
end_location:
row: 38
column: 36
fix:
edits:
- content: "\\u200b"
location:
row: 38
column: 35
end_location:
row: 38
column: 36
parent: ~
- kind:
name: InvalidCharacterZeroWidthSpace
body: "Invalid unescaped character zero-width-space, use \"\\u200B\" instead"
suggestion: Replace with escape sequence
fixable: true
location:
row: 39
column: 59
end_location:
row: 39
column: 60
fix:
edits:
- content: "\\u200b"
location:
row: 39
column: 59
end_location:
row: 39
column: 60
parent: ~
- kind:
name: InvalidCharacterZeroWidthSpace
body: "Invalid unescaped character zero-width-space, use \"\\u200B\" instead"
suggestion: Replace with escape sequence
fixable: true
location:
row: 39
column: 60
end_location:
row: 39
column: 61
fix:
edits:
- content: "\\u200b"
location:
row: 39
column: 60
end_location:
row: 39
column: 61
parent: ~