From 48d8680e71e33cef1ec9560bdb5c33ca7783c56f Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 31 Mar 2023 19:03:00 +0200 Subject: [PATCH] Ambiguous unicode, only test unicode characters (#3814) --- .../ruff/rules/ambiguous_unicode_character.rs | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs b/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs index 22a7258c09..3c661302c0 100644 --- a/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs +++ b/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs @@ -1698,45 +1698,47 @@ pub fn ambiguous_unicode_character( let mut col_offset = 0; let mut row_offset = 0; for current_char in text.chars() { - // Search for confusing characters. - if let Some(representant) = CONFUSABLES.get(&(current_char as u32)) { - if !settings.allowed_confusables.contains(¤t_char) { - let col = if row_offset == 0 { - start.column() + col_offset - } else { - col_offset - }; - let location = Location::new(start.row() + row_offset, col); - let end_location = Location::new(location.row(), location.column() + 1); - let mut diagnostic = Diagnostic::new::( - match context { - Context::String => AmbiguousUnicodeCharacterString { - confusable: current_char, - representant: *representant as char, + if !current_char.is_ascii() { + // Search for confusing characters. + if let Some(representant) = CONFUSABLES.get(&(current_char as u32)).copied() { + if !settings.allowed_confusables.contains(¤t_char) { + let col = if row_offset == 0 { + start.column() + col_offset + } else { + col_offset + }; + let location = Location::new(start.row() + row_offset, col); + let end_location = Location::new(location.row(), location.column() + 1); + let mut diagnostic = Diagnostic::new::( + match context { + Context::String => AmbiguousUnicodeCharacterString { + confusable: current_char, + representant: representant as char, + } + .into(), + Context::Docstring => AmbiguousUnicodeCharacterDocstring { + confusable: current_char, + representant: representant as char, + } + .into(), + Context::Comment => AmbiguousUnicodeCharacterComment { + confusable: current_char, + representant: representant as char, + } + .into(), + }, + Range::new(location, end_location), + ); + if settings.rules.enabled(diagnostic.kind.rule()) { + if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) { + diagnostic.set_fix(Edit::replacement( + (representant as char).to_string(), + location, + end_location, + )); } - .into(), - Context::Docstring => AmbiguousUnicodeCharacterDocstring { - confusable: current_char, - representant: *representant as char, - } - .into(), - Context::Comment => AmbiguousUnicodeCharacterComment { - confusable: current_char, - representant: *representant as char, - } - .into(), - }, - Range::new(location, end_location), - ); - if settings.rules.enabled(diagnostic.kind.rule()) { - if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::replacement( - (*representant as char).to_string(), - location, - end_location, - )); + diagnostics.push(diagnostic); } - diagnostics.push(diagnostic); } } }