mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-20 18:40:28 +00:00
Introduce LiteralExpressionRef
for all literals (#8339)
## Summary This PR adds a new `LiteralExpressionRef` which wraps all of the literal expression nodes in a single enum. This allows for a narrow type when working exclusively with a literal node. Additionally, it also implements a `Expr::as_literal_expr` method to return the new enum if the expression is indeed a literal one. A few rules have been updated to account for the new enum: 1. `redundant_literal_union` 2. `if_else_block_instead_of_dict_lookup` 3. `magic_value_comparison` To account for the change in (2), a new `ComparableLiteral` has been added which can be constructed from the new enum (`ComparableLiteral::from(<LiteralExpressionRef>)`). ### Open Questions 1. The new `ComparableLiteral` can be exclusively used via the `LiteralExpressionRef` enum. Should we remove all of the literal variants from `ComparableExpr` and instead have a single `ComparableExpr::Literal(ComparableLiteral)` variant instead? ## Test Plan `cargo test`
This commit is contained in:
parent
a8d04cbd88
commit
97ae617fac
7 changed files with 127 additions and 48 deletions
|
@ -6,7 +6,7 @@ use std::fmt;
|
|||
use std::fmt::Debug;
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::int;
|
||||
use crate::{int, LiteralExpressionRef};
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)
|
||||
|
@ -640,6 +640,18 @@ impl Expr {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn as_literal_expr(&self) -> Option<LiteralExpressionRef<'_>> {
|
||||
match self {
|
||||
Expr::StringLiteral(expr) => Some(LiteralExpressionRef::StringLiteral(expr)),
|
||||
Expr::BytesLiteral(expr) => Some(LiteralExpressionRef::BytesLiteral(expr)),
|
||||
Expr::NumberLiteral(expr) => Some(LiteralExpressionRef::NumberLiteral(expr)),
|
||||
Expr::BooleanLiteral(expr) => Some(LiteralExpressionRef::BooleanLiteral(expr)),
|
||||
Expr::NoneLiteral(expr) => Some(LiteralExpressionRef::NoneLiteral(expr)),
|
||||
Expr::EllipsisLiteral(expr) => Some(LiteralExpressionRef::EllipsisLiteral(expr)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_implicit_concatenated_string(&self) -> bool {
|
||||
match self {
|
||||
Expr::StringLiteral(ExprStringLiteral {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue