ruff/crates/ruff_python_ast/src
Alex Waygood b6b1947010
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`
2025-02-17 07:58:54 +00:00
..
visitor Auto-generate AST boilerplate (#15544) 2025-01-17 14:23:02 -05:00
comparable.rs Respect hash-equivalent literals in `iteration-over-set` (#14063) 2024-11-03 18:44:52 +00:00
docstrings.rs Move Python whitespace utilities into new `ruff_python_whitespace` crate (#4993) 2023-06-10 00:59:57 +00:00
expression.rs Remove customizable reference enum names (#15647) 2025-01-21 13:46:31 -05:00
generated.rs Replace is-macro with implementation in enums (#16144) 2025-02-13 22:49:00 +00:00
helpers.rs [`flake8-annotations`] Correct syntax for `typing.Union` in suggested return type fixes for `ANN20x` rules (#16025) 2025-02-07 17:17:20 -06:00
identifier.rs Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
int.rs [`ruff`] Unnecessary cast to `int` (`RUF046`) (#14697) 2024-12-05 10:30:06 +01:00
lib.rs Move `red_knot_python_semantic::PythonVersion` to the `ruff_python_ast` crate (#16147) 2025-02-14 12:48:08 -05:00
name.rs Transition to salsa coarse-grained tracked structs (#15763) 2025-02-11 11:38:50 +01:00
node.rs Remove `AstNode` and `AnyNode` (#15479) 2025-01-17 17:11:00 -05:00
nodes.rs Improve API exposed on `ExprStringLiteral` nodes (#16192) 2025-02-17 07:58:54 +00:00
operator_precedence.rs [internal] Move Linter `OperatorPrecedence` into `ruff_python_ast` crate (#16162) 2025-02-14 15:55:07 +01:00
parenthesize.rs Remove customizable reference enum names (#15647) 2025-01-21 13:46:31 -05:00
python_version.rs Move `red_knot_python_semantic::PythonVersion` to the `ruff_python_ast` crate (#16147) 2025-02-14 12:48:08 -05:00
relocate.rs Remove `Expr` postfix from `ExprNamed`, `ExprIf`, and `ExprGenerator` (#10229) 2024-03-04 12:55:01 +01:00
script.rs Skip namespace package enforcement for PEP 723 scripts (#13974) 2024-10-29 02:11:31 +00:00
statement_visitor.rs Remove `Stmt::TryStar` (#6566) 2023-08-14 13:39:44 -04:00
stmt_if.rs Misc. small tweaks from perusing modules (#9383) 2024-01-03 12:30:25 -05:00
str.rs Preserve triple quotes and prefixes for strings (#15818) 2025-02-04 08:41:06 -05:00
str_prefix.rs Reduce memory usage of `Docstring` struct (#16183) 2025-02-16 15:23:52 +00:00
traversal.rs Use referencial equality in `traversal` helper methods (#13895) 2024-10-24 11:30:22 +02:00
types.rs Remove `RefEquality` (#6393) 2023-08-07 16:04:50 +00:00
visitor.rs Simplify iteration idioms (#13834) 2024-10-20 22:25:27 +01:00
whitespace.rs Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00