Skip to content

Commit

Permalink
FStringPart -> FStringElement etc
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten committed Aug 28, 2023
1 parent cdebe7c commit 0b25e61
Show file tree
Hide file tree
Showing 51 changed files with 425 additions and 399 deletions.
6 changes: 3 additions & 3 deletions crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,15 +932,15 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
pylint::rules::await_outside_async(checker, expr);
}
}
Expr::FString(ast::ExprFString { parts, .. }) => {
Expr::FString(ast::ExprFString { elements, .. }) => {
if checker.enabled(Rule::FStringMissingPlaceholders) {
pyflakes::rules::f_string_missing_placeholders(expr, parts, checker);
pyflakes::rules::f_string_missing_placeholders(expr, elements, checker);
}
if checker.enabled(Rule::HardcodedSQLExpression) {
flake8_bandit::rules::hardcoded_sql_expression(checker, expr);
}
if checker.enabled(Rule::ExplicitFStringTypeConversion) {
ruff::rules::explicit_f_string_type_conversion(checker, expr, parts);
ruff::rules::explicit_f_string_type_conversion(checker, expr, elements);
}
}
Expr::BinOp(ast::ExprBinOp {
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff/src/rules/flake8_pytest_style/rules/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ pub(super) fn is_empty_or_null_string(expr: &Expr) -> bool {
..
}) => string.is_empty(),
Expr::Constant(constant) if constant.value.is_none() => true,
Expr::FString(ast::ExprFString { parts, .. }) => {
parts.iter().all(is_empty_or_null_string_part)
Expr::FString(ast::ExprFString { elements, .. }) => {
elements.iter().all(is_empty_or_null_fstring_element)
}
_ => false,
}
}

fn is_empty_or_null_string_part(part: &ast::FStringPart) -> bool {
match part {
ast::FStringPart::Literal(ast::PartialString { value, .. }) => value.is_empty(),
ast::FStringPart::FormattedValue(ast::FormattedValue { expression, .. }) => {
fn is_empty_or_null_fstring_element(element: &ast::FStringElement) -> bool {
match element {
ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => value.is_empty(),
ast::FStringElement::Expression(ast::FStringExpressionElement { expression, .. }) => {
is_empty_or_null_string(expression)
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff/src/rules/flynt/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ruff_python_ast::{self as ast, Arguments, Constant, ConversionFlag, Expr};
use ruff_text_size::TextRange;

/// Wrap an expression in a `FormattedValue` with no special formatting.
fn to_formatted_value_expr(inner: &Expr) -> ast::FStringPart {
ast::FStringPart::FormattedValue(ast::FormattedValue {
/// Wrap an expression in a `FStringExpressionElement` with no special formatting.
fn to_formatted_value_expr(inner: &Expr) -> ast::FStringElement {
ast::FStringElement::Expression(ast::FStringExpressionElement {
expression: Box::new(inner.clone()),
debug_text: None,
conversion: ConversionFlag::None,
Expand All @@ -13,8 +13,8 @@ fn to_formatted_value_expr(inner: &Expr) -> ast::FStringPart {
}

/// Convert a string to a constant string expression.
pub(super) fn to_constant_string(s: &str) -> ast::FStringPart {
ast::FStringPart::Literal(ast::PartialString {
pub(super) fn to_constant_string(s: &str) -> ast::FStringElement {
ast::FStringElement::Literal(ast::FStringLiteralElement {
value: s.to_owned(),
range: TextRange::default(),
})
Expand Down Expand Up @@ -49,14 +49,14 @@ fn is_simple_callee(func: &Expr) -> bool {
}

/// Convert an expression to a f-string element (if it looks like a good idea).
pub(super) fn to_fstring_part(expr: &Expr) -> Option<ast::FStringPart> {
pub(super) fn to_fstring_element(expr: &Expr) -> Option<ast::FStringElement> {
match expr {
// These are directly handled by `unparse_f_string_element`:
Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
range,
..
}) => Some(ast::FStringPart::Literal(ast::PartialString {
}) => Some(ast::FStringElement::Literal(ast::FStringLiteralElement {
value: value.to_string(),
range: *range,
})),
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
return Some(node.into());
}

let mut fstring_parts = Vec::with_capacity(joinees.len() * 2);
let mut fstring_elements = Vec::with_capacity(joinees.len() * 2);
let mut first = true;

for expr in joinees {
Expand All @@ -101,13 +101,13 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
return None;
}
if !std::mem::take(&mut first) {
fstring_parts.push(helpers::to_constant_string(joiner));
fstring_elements.push(helpers::to_constant_string(joiner));
}
fstring_parts.push(helpers::to_fstring_part(expr)?);
fstring_elements.push(helpers::to_fstring_element(expr)?);
}

let node = ast::ExprFString {
parts: fstring_parts,
elements: fstring_elements,
implicit_concatenated: false,
range: TextRange::default(),
};
Expand Down Expand Up @@ -140,7 +140,7 @@ pub(crate) fn static_join_to_fstring(checker: &mut Checker, expr: &Expr, joiner:
};

// Try to build the fstring (internally checks whether e.g. the elements are
// convertible to f-string parts).
// convertible to f-string elements).
let Some(new_expr) = build_fstring(joiner, joinees) else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Expr, FStringPart, PySourceType};
use ruff_python_ast::{Expr, FStringElement, PySourceType};
use ruff_python_parser::{lexer, AsMode, StringKind, Tok};
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange, TextSize};
Expand Down Expand Up @@ -81,12 +81,12 @@ fn find_useless_f_strings<'a>(
/// F541
pub(crate) fn f_string_missing_placeholders(
expr: &Expr,
values: &[FStringPart],
values: &[FStringElement],
checker: &mut Checker,
) {
if !values
.iter()
.any(|value| matches!(value, FStringPart::FormattedValue(_)))
.any(|value| matches!(value, FStringElement::Expression(_)))
{
for (prefix_range, tok_range) in
find_useless_f_strings(expr, checker.locator(), checker.source_type)
Expand Down
22 changes: 11 additions & 11 deletions crates/ruff/src/rules/pylint/rules/assert_on_string_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) {
}
_ => {}
},
Expr::FString(ast::ExprFString { parts, .. }) => {
Expr::FString(ast::ExprFString { elements, .. }) => {
checker.diagnostics.push(Diagnostic::new(
AssertOnStringLiteral {
kind: if parts.iter().all(|part| match part {
ast::FStringPart::Literal(ast::PartialString { value, .. }) => {
value.is_empty()
}
ast::FStringPart::FormattedValue(_) => false,
kind: if elements.iter().all(|element| match element {
ast::FStringElement::Literal(ast::FStringLiteralElement {
value, ..
}) => value.is_empty(),
ast::FStringElement::Expression(_) => false,
}) {
Kind::Empty
} else if parts.iter().any(|part| match part {
ast::FStringPart::Literal(ast::PartialString { value, .. }) => {
!value.is_empty()
}
ast::FStringPart::FormattedValue(_) => false,
} else if elements.iter().any(|element| match element {
ast::FStringElement::Literal(ast::FStringLiteralElement {
value, ..
}) => !value.is_empty(),
ast::FStringElement::Expression(_) => false,
}) {
Kind::NonEmpty
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ impl AlwaysAutofixableViolation for ExplicitFStringTypeConversion {
pub(crate) fn explicit_f_string_type_conversion(
checker: &mut Checker,
expr: &Expr,
values: &[ast::FStringPart],
values: &[ast::FStringElement],
) {
for (index, formatted_value) in values
.iter()
.filter_map(|expr| {
if let ast::FStringPart::FormattedValue(expr) = &expr {
if let ast::FStringElement::Expression(expr) = &expr {
Some(expr)
} else {
None
}
})
.enumerate()
{
let ast::FormattedValue {
let ast::FStringExpressionElement {
expression,
conversion,
..
Expand Down
8 changes: 5 additions & 3 deletions crates/ruff/src/rules/tryceratops/rules/raise_vanilla_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ pub(crate) fn raise_vanilla_args(checker: &mut Checker, expr: &Expr) {
/// some whitespace).
fn contains_message(expr: &Expr) -> bool {
match expr {
Expr::FString(ast::ExprFString { parts, .. }) => {
for part in parts {
if let ast::FStringPart::Literal(ast::PartialString { value, .. }) = part {
Expr::FString(ast::ExprFString { elements, .. }) => {
for element in elements {
if let ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) =
element
{
if value.chars().any(char::is_whitespace) {
return true;
}
Expand Down
32 changes: 17 additions & 15 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,27 @@ impl<'a> From<&'a ast::ExceptHandler> for ComparableExceptHandler<'a> {
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ComparableFStringPart<'a> {
pub enum ComparableFStringElement<'a> {
Literal(&'a str),
FormattedValue(FormattedValue<'a>),
FStringExpressionElement(FStringExpressionElement<'a>),
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct FormattedValue<'a> {
pub struct FStringExpressionElement<'a> {
expression: ComparableExpr<'a>,
debug_text: Option<&'a ast::DebugText>,
conversion: ast::ConversionFlag,
format_spec: Vec<ComparableFStringPart<'a>>,
format_spec: Vec<ComparableFStringElement<'a>>,
}

impl<'a> From<&'a ast::FStringPart> for ComparableFStringPart<'a> {
fn from(fstring_part: &'a ast::FStringPart) -> Self {
impl<'a> From<&'a ast::FStringElement> for ComparableFStringElement<'a> {
fn from(fstring_part: &'a ast::FStringElement) -> Self {
match fstring_part {
ast::FStringPart::Literal(ast::PartialString { value, .. }) => Self::Literal(value),
ast::FStringPart::FormattedValue(formatted_value) => {
Self::FormattedValue(FormattedValue {
ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => {
Self::Literal(value)
}
ast::FStringElement::Expression(formatted_value) => {
Self::FStringExpressionElement(FStringExpressionElement {
expression: (&formatted_value.expression).into(),
debug_text: formatted_value.debug_text.as_ref(),
conversion: formatted_value.conversion,
Expand Down Expand Up @@ -670,16 +672,16 @@ pub struct ExprCall<'a> {
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprFormattedValue<'a> {
pub struct ExprFStringExpressionElement<'a> {
value: Box<ComparableExpr<'a>>,
debug_text: Option<&'a ast::DebugText>,
conversion: ast::ConversionFlag,
format_spec: Vec<ComparableFStringPart<'a>>,
format_spec: Vec<ComparableFStringElement<'a>>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprFString<'a> {
parts: Vec<ComparableFStringPart<'a>>,
elements: Vec<ComparableFStringElement<'a>>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -758,7 +760,7 @@ pub enum ComparableExpr<'a> {
YieldFrom(ExprYieldFrom<'a>),
Compare(ExprCompare<'a>),
Call(ExprCall<'a>),
FormattedValue(ExprFormattedValue<'a>),
FStringExpressionElement(ExprFStringExpressionElement<'a>),
FString(ExprFString<'a>),
Constant(ExprConstant<'a>),
Attribute(ExprAttribute<'a>),
Expand Down Expand Up @@ -916,11 +918,11 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
arguments: arguments.into(),
}),
ast::Expr::FString(ast::ExprFString {
parts,
elements,
implicit_concatenated: _,
range: _,
}) => Self::FString(ExprFString {
parts: parts.iter().map(Into::into).collect(),
elements: elements.iter().map(Into::into).collect(),
}),
ast::Expr::Constant(ast::ExprConstant {
value,
Expand Down
30 changes: 15 additions & 15 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use ruff_text_size::{Ranged, TextRange};
use crate::call_path::CallPath;
use crate::statement_visitor::{walk_body, walk_stmt, StatementVisitor};
use crate::{
self as ast, Arguments, Constant, ExceptHandler, Expr, FStringPart, MatchCase, Pattern, Stmt,
TypeParam,
self as ast, Arguments, Constant, ExceptHandler, Expr, FStringElement, MatchCase, Pattern,
Stmt, TypeParam,
};

/// Return `true` if the `Stmt` is a compound statement (as opposed to a simple statement).
Expand Down Expand Up @@ -122,9 +122,9 @@ pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool {
Expr::BoolOp(ast::ExprBoolOp { values, .. }) => {
values.iter().any(|expr| any_over_expr(expr, func))
}
Expr::FString(ast::ExprFString { parts, .. }) => {
parts.iter().any(|part| any_over_fstring_part(part, func))
}
Expr::FString(ast::ExprFString { elements, .. }) => elements
.iter()
.any(|element| any_over_fstring_element(element, func)),
Expr::NamedExpr(ast::ExprNamedExpr {
target,
value,
Expand Down Expand Up @@ -285,18 +285,18 @@ pub fn any_over_pattern(pattern: &Pattern, func: &dyn Fn(&Expr) -> bool) -> bool
}
}

pub fn any_over_fstring_part(part: &FStringPart, func: &dyn Fn(&Expr) -> bool) -> bool {
match part {
FStringPart::Literal(_) => false,
FStringPart::FormattedValue(ast::FormattedValue {
pub fn any_over_fstring_element(element: &FStringElement, func: &dyn Fn(&Expr) -> bool) -> bool {
match element {
FStringElement::Literal(_) => false,
FStringElement::Expression(ast::FStringExpressionElement {
expression,
format_spec,
..
}) => {
any_over_expr(expression, func)
|| format_spec
.iter()
.any(|spec_part| any_over_fstring_part(spec_part, func))
.any(|spec_element| any_over_fstring_element(spec_element, func))
}
}
}
Expand Down Expand Up @@ -1077,14 +1077,14 @@ impl Truthiness {
Constant::Complex { real, imag } => Some(*real != 0.0 || *imag != 0.0),
Constant::Ellipsis => Some(true),
},
Expr::FString(ast::ExprFString { parts, .. }) => {
if parts.is_empty() {
Expr::FString(ast::ExprFString { elements, .. }) => {
if elements.is_empty() {
Some(false)
} else if parts.iter().any(|part| match part {
ast::FStringPart::Literal(ast::PartialString { value, .. }) => {
} else if elements.iter().any(|element| match element {
ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => {
!value.is_empty()
}
ast::FStringPart::FormattedValue(_) => true,
ast::FStringElement::Expression(_) => true,
}) {
Some(true)
} else {
Expand Down
Loading

0 comments on commit 0b25e61

Please sign in to comment.