[ruff] Trigger RUF037 for empty string and byte strings (#18862)

This commit is contained in:
Victor Hugo Gomes
2025-06-24 03:26:28 -03:00
committed by GitHub
parent e474f36473
commit ca7933804e
4 changed files with 145 additions and 1 deletions

View File

@@ -504,6 +504,20 @@ impl FStringValue {
pub fn elements(&self) -> impl Iterator<Item = &InterpolatedStringElement> {
self.f_strings().flat_map(|fstring| fstring.elements.iter())
}
/// Returns `true` if the node represents an empty f-string literal.
///
/// Noteh that a [`FStringValue`] node will always have >= 1 [`FStringPart`]s inside it.
/// This method checks whether the value of the concatenated parts is equal to the empty
/// f-string, not whether the f-string has 0 parts inside it.
pub fn is_empty_literal(&self) -> bool {
match &self.inner {
FStringValueInner::Single(fstring_part) => fstring_part.is_empty_literal(),
FStringValueInner::Concatenated(fstring_parts) => {
fstring_parts.iter().all(FStringPart::is_empty_literal)
}
}
}
}
impl<'a> IntoIterator for &'a FStringValue {
@@ -550,6 +564,13 @@ impl FStringPart {
Self::FString(f_string) => f_string.flags.quote_style(),
}
}
pub fn is_empty_literal(&self) -> bool {
match &self {
FStringPart::Literal(string_literal) => string_literal.value.is_empty(),
FStringPart::FString(f_string) => f_string.elements.is_empty(),
}
}
}
impl Ranged for FStringPart {