Improve API exposed on ExprStringLiteral nodes (#16192)

## Summary

This PR makes the following changes:
- It adjusts various callsites to use the new
`ast::StringLiteral::contents_range()` method that was introduced in
https://github.com/astral-sh/ruff/pull/16183. This is less verbose and
more type-safe than using the `ast::str::raw_contents()` helper
function.
- It adds a new `ast::ExprStringLiteral::as_unconcatenated_literal()`
helper method, and adjusts various callsites to use it. This addresses
@MichaReiser's review comment at
https://github.com/astral-sh/ruff/pull/16183#discussion_r1957334365.
There is no functional change here, but it helps readability to make it
clearer that we're differentiating between implicitly concatenated
strings and unconcatenated strings at various points.
- It renames the `StringLiteralValue::flags()` method to
`StringLiteralFlags::first_literal_flags()`. If you're dealing with an
implicitly concatenated string `string_node`,
`string_node.value.flags().closer_len()` could give an incorrect result;
this renaming makes it clearer that the `StringLiteralFlags` instance
returned by the method is only guaranteed to give accurate information
for the first `StringLiteral` contained in the `ExprStringLiteral` node.
- It deletes the unused `BytesLiteralValue::flags()` method. This seems
prone to misuse in the same way as `StringLiteralValue::flags()`: if
it's an implicitly concatenated bytestring, the `BytesLiteralFlags`
instance returned by the method would only give accurate information for
the first `BytesLiteral` in the bytestring.

## Test Plan

`cargo test`
This commit is contained in:
Alex Waygood 2025-02-17 07:58:54 +00:00 committed by GitHub
parent 21999b3be7
commit b6b1947010
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 37 additions and 43 deletions

View file

@ -182,9 +182,8 @@ pub(crate) fn definitions(checker: &mut Checker) {
continue;
};
// If the `ExprStringLiteral` has multiple parts, it is implicitly concatenated.
// We don't support recognising such strings as docstrings in our model currently.
let [sole_string_part] = string_literal.value.as_slice() else {
// We don't recognise implicitly concatenated strings as valid docstrings in our model currently.
let Some(sole_string_part) = string_literal.as_unconcatenated_literal() else {
#[allow(deprecated)]
let location = checker
.locator

View file

@ -1537,7 +1537,7 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
}
}
if checker.enabled(Rule::MissingFStringSyntax) {
for string_literal in value.as_slice() {
for string_literal in value {
ruff::rules::missing_fstring_syntax(checker, string_literal);
}
}

View file

@ -159,11 +159,17 @@ fn split_default(str_value: &StringLiteralValue, max_split: i32) -> Option<Expr>
}
Ordering::Equal => {
let list_items: Vec<&str> = vec![str_value.to_str()];
Some(construct_replacement(&list_items, str_value.flags()))
Some(construct_replacement(
&list_items,
str_value.first_literal_flags(),
))
}
Ordering::Less => {
let list_items: Vec<&str> = str_value.to_str().split_whitespace().collect();
Some(construct_replacement(&list_items, str_value.flags()))
Some(construct_replacement(
&list_items,
str_value.first_literal_flags(),
))
}
}
}
@ -187,7 +193,7 @@ fn split_sep(
}
};
construct_replacement(&list_items, str_value.flags())
construct_replacement(&list_items, str_value.first_literal_flags())
}
/// Returns the value of the `maxsplit` argument as an `i32`, if it is a numeric value.

View file

@ -72,7 +72,7 @@ fn build_fstring(joiner: &str, joinees: &[Expr], flags: FStringFlags) -> Option<
if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = expr {
if flags.is_none() {
// take the flags from the first Expr
flags = Some(value.flags());
flags = Some(value.first_literal_flags());
}
Some(value.to_str())
} else {