Skip to content

Commit

Permalink
Convert confusable violations to named fields (#1887)
Browse files Browse the repository at this point in the history
See: #1871.
  • Loading branch information
charliermarsh committed Jan 15, 2023
1 parent 81996f1 commit 2c64461
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
18 changes: 9 additions & 9 deletions src/rules/ruff/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,20 +1632,20 @@ pub fn ambiguous_unicode_character(
let end_location = Location::new(location.row(), location.column() + 1);
let mut diagnostic = Diagnostic::new::<DiagnosticKind>(
match context {
Context::String => violations::AmbiguousUnicodeCharacterString(
current_char,
Context::String => violations::AmbiguousUnicodeCharacterString {
confusable: current_char,
representant,
)
}
.into(),
Context::Docstring => violations::AmbiguousUnicodeCharacterDocstring(
current_char,
Context::Docstring => violations::AmbiguousUnicodeCharacterDocstring {
confusable: current_char,
representant,
)
}
.into(),
Context::Comment => violations::AmbiguousUnicodeCharacterComment(
current_char,
Context::Comment => violations::AmbiguousUnicodeCharacterComment {
confusable: current_char,
representant,
)
}
.into(),
},
Range::new(location, end_location),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ expression: diagnostics
---
- kind:
AmbiguousUnicodeCharacterString:
- 𝐁
- B
confusable: 𝐁
representant: B
location:
row: 1
column: 5
Expand All @@ -23,8 +23,8 @@ expression: diagnostics
parent: ~
- kind:
AmbiguousUnicodeCharacterDocstring:
-
- )
confusable:
representant: )
location:
row: 6
column: 55
Expand All @@ -42,8 +42,8 @@ expression: diagnostics
parent: ~
- kind:
AmbiguousUnicodeCharacterComment:
-
- /
confusable:
representant: /
location:
row: 7
column: 61
Expand Down
60 changes: 48 additions & 12 deletions src/violations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6030,68 +6030,104 @@ impl AlwaysAutofixableViolation for PreferListBuiltin {
// Ruff

define_violation!(
pub struct AmbiguousUnicodeCharacterString(pub char, pub char);
pub struct AmbiguousUnicodeCharacterString {
pub confusable: char,
pub representant: char,
}
);
impl AlwaysAutofixableViolation for AmbiguousUnicodeCharacterString {
fn message(&self) -> String {
let AmbiguousUnicodeCharacterString(confusable, representant) = self;
let AmbiguousUnicodeCharacterString {
confusable,
representant,
} = self;
format!(
"String contains ambiguous unicode character '{confusable}' (did you mean \
'{representant}'?)"
)
}

fn autofix_title(&self) -> String {
let AmbiguousUnicodeCharacterString(confusable, representant) = self;
let AmbiguousUnicodeCharacterString {
confusable,
representant,
} = self;
format!("Replace '{confusable}' with '{representant}'")
}

fn placeholder() -> Self {
AmbiguousUnicodeCharacterString('𝐁', 'B')
AmbiguousUnicodeCharacterString {
confusable: '𝐁',
representant: 'B',
}
}
}

define_violation!(
pub struct AmbiguousUnicodeCharacterDocstring(pub char, pub char);
pub struct AmbiguousUnicodeCharacterDocstring {
pub confusable: char,
pub representant: char,
}
);
impl AlwaysAutofixableViolation for AmbiguousUnicodeCharacterDocstring {
fn message(&self) -> String {
let AmbiguousUnicodeCharacterDocstring(confusable, representant) = self;
let AmbiguousUnicodeCharacterDocstring {
confusable,
representant,
} = self;
format!(
"Docstring contains ambiguous unicode character '{confusable}' (did you mean \
'{representant}'?)"
)
}

fn autofix_title(&self) -> String {
let AmbiguousUnicodeCharacterDocstring(confusable, representant) = self;
let AmbiguousUnicodeCharacterDocstring {
confusable,
representant,
} = self;
format!("Replace '{confusable}' with '{representant}'")
}

fn placeholder() -> Self {
AmbiguousUnicodeCharacterDocstring('𝐁', 'B')
AmbiguousUnicodeCharacterDocstring {
confusable: '𝐁',
representant: 'B',
}
}
}

define_violation!(
pub struct AmbiguousUnicodeCharacterComment(pub char, pub char);
pub struct AmbiguousUnicodeCharacterComment {
pub confusable: char,
pub representant: char,
}
);
impl AlwaysAutofixableViolation for AmbiguousUnicodeCharacterComment {
fn message(&self) -> String {
let AmbiguousUnicodeCharacterComment(confusable, representant) = self;
let AmbiguousUnicodeCharacterComment {
confusable,
representant,
} = self;
format!(
"Comment contains ambiguous unicode character '{confusable}' (did you mean \
'{representant}'?)"
)
}

fn autofix_title(&self) -> String {
let AmbiguousUnicodeCharacterComment(confusable, representant) = self;
let AmbiguousUnicodeCharacterComment {
confusable,
representant,
} = self;
format!("Replace '{confusable}' with '{representant}'")
}

fn placeholder() -> Self {
AmbiguousUnicodeCharacterComment('𝐁', 'B')
AmbiguousUnicodeCharacterComment {
confusable: '𝐁',
representant: 'B',
}
}
}

Expand Down

0 comments on commit 2c64461

Please sign in to comment.