Explicitly support ASCII-only for capitalization checks (#4290)

This commit is contained in:
Charlie Marsh 2023-05-08 15:41:11 -04:00 committed by GitHub
parent 4ac506526b
commit b913e99bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 15 deletions

View file

@ -16,3 +16,12 @@ def utf8_function():
def uppercase_char_not_possible():
"""'args' is not capitalized."""
def non_alphabetic():
"""th!is is not capitalized."""
def non_ascii():
"""th•s is not capitalized."""
def all_caps():
"""th•s is not capitalized."""

View file

@ -40,36 +40,27 @@ pub fn capitalized(checker: &mut Checker, docstring: &Docstring) {
}
let body = docstring.body();
let Some(first_word) = body.split(' ').next() else {
return
};
if first_word == first_word.to_uppercase() {
return;
}
// Like pydocstyle, we only support ASCII for now.
for char in first_word.chars() {
if !char.is_ascii_alphabetic() && char != '\'' {
return;
}
}
let mut first_word_chars = first_word.chars();
let Some(first_char) = first_word_chars.next() else {
return;
};
if first_char.is_uppercase() {
return;
};
if first_char
.to_uppercase()
.next()
.map_or(false, |uppercase_first_char| {
uppercase_first_char == first_char
})
{
let uppercase_first_char = first_char.to_ascii_uppercase();
if first_char == uppercase_first_char {
return;
}
let capitalized_word = first_char.to_uppercase().to_string() + first_word_chars.as_str();
let capitalized_word = uppercase_first_char.to_string() + first_word_chars.as_str();
let mut diagnostic = Diagnostic::new(
FirstLineCapitalized {