Skip to content

Commit

Permalink
Rename JoinedStr to FString in the AST (#6379)
Browse files Browse the repository at this point in the history
## Summary

Per the proposal in #6183,
this PR renames the `JoinedStr` node to `FString`.
  • Loading branch information
charliermarsh authored Aug 7, 2023
1 parent 999d88e commit 3f0eea6
Show file tree
Hide file tree
Showing 56 changed files with 166 additions and 166 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
pylint::rules::await_outside_async(checker, expr);
}
}
Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Expr::FString(ast::ExprFString { values, range: _ }) => {
if checker.enabled(Rule::FStringMissingPlaceholders) {
pyflakes::rules::f_string_missing_placeholders(expr, values, checker);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ where
));
}
}
Expr::JoinedStr(_) => {
Expr::FString(_) => {
self.semantic.flags |= SemanticModelFlags::F_STRING;
visitor::walk_expr(self, expr);
}
Expand Down Expand Up @@ -1276,7 +1276,7 @@ where

fn visit_format_spec(&mut self, format_spec: &'b Expr) {
match format_spec {
Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Expr::FString(ast::ExprFString { values, range: _ }) => {
for value in values {
self.visit_expr(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn matches_string_format_expression(expr: &Expr, model: &SemanticModel) -> bool
attr == "format" && string_literal(value).is_some()
}
// f"select * from table where val = {val}"
Expr::JoinedStr(_) => true,
Expr::FString(_) => true,
_ => false,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn f_string_docstring(checker: &mut Checker, body: &[Stmt]) {
let Stmt::Expr(ast::StmtExpr { value, range: _ }) = stmt else {
return;
};
if !value.is_joined_str_expr() {
if !value.is_f_string_expr() {
return;
}
checker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
// Ignore strings, to avoid false positives with docstrings.
if matches!(
value,
Expr::JoinedStr(_)
Expr::FString(_)
| Expr::Constant(ast::ExprConstant {
value: Constant::Str(..) | Constant::Ellipsis,
..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
}
}
// Check for f-strings.
Expr::JoinedStr(_) => {
Expr::FString(_) => {
if checker.enabled(Rule::FStringInException) {
let mut diagnostic = Diagnostic::new(FStringInException, first.range());
if checker.patch(diagnostic.kind.rule()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Violation for FStringInGetTextFuncCall {
/// INT001
pub(crate) fn f_string_in_gettext_func_call(checker: &mut Checker, args: &[Expr]) {
if let Some(first) = args.first() {
if first.is_joined_str_expr() {
if first.is_f_string_expr() {
checker
.diagnostics
.push(Diagnostic::new(FStringInGetTextFuncCall {}, first.range()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ pub(crate) fn explicit(expr: &Expr, locator: &Locator) -> Option<Diagnostic> {
if matches!(op, Operator::Add) {
if matches!(
left.as_ref(),
Expr::JoinedStr(_)
Expr::FString(_)
| Expr::Constant(ast::ExprConstant {
value: Constant::Str(..) | Constant::Bytes(..),
..
})
) && matches!(
right.as_ref(),
Expr::JoinedStr(_)
Expr::FString(_)
| Expr::Constant(ast::ExprConstant {
value: Constant::Str(..) | Constant::Bytes(..),
..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn check_msg(checker: &mut Checker, msg: &Expr) {
_ => {}
},
// Check for f-strings.
Expr::JoinedStr(_) => {
Expr::FString(_) => {
if checker.enabled(Rule::LoggingFString) {
checker
.diagnostics
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_pytest_style/rules/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(super) fn is_empty_or_null_string(expr: &Expr) -> bool {
..
}) => string.is_empty(),
Expr::Constant(constant) if constant.value.is_none() => true,
Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Expr::FString(ast::ExprFString { values, range: _ }) => {
values.iter().all(is_empty_or_null_string)
}
_ => false,
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flynt/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,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_elem(expr: &Expr) -> Option<Expr> {
pub(super) fn to_f_string_element(expr: &Expr) -> Option<Expr> {
match expr {
// These are directly handled by `unparse_fstring_elem`:
// These are directly handled by `unparse_f_string_element`:
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
})
| Expr::JoinedStr(_)
| Expr::FString(_)
| Expr::FormattedValue(_) => Some(expr.clone()),
// These should be pretty safe to wrap in a formatted value.
Expr::Constant(ast::ExprConstant {
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
let mut first = true;

for expr in joinees {
if expr.is_joined_str_expr() {
if expr.is_f_string_expr() {
// Oops, already an f-string. We don't know how to handle those
// gracefully right now.
return None;
}
if !std::mem::take(&mut first) {
fstring_elems.push(helpers::to_constant_string(joiner));
}
fstring_elems.push(helpers::to_fstring_elem(expr)?);
fstring_elems.push(helpers::to_f_string_element(expr)?);
}

let node = ast::ExprJoinedStr {
let node = ast::ExprFString {
values: fstring_elems,
range: TextRange::default(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl AlwaysAutofixableViolation for FStringMissingPlaceholders {
}
}

/// Find f-strings that don't contain any formatted values in a `JoinedStr`.
/// Find f-strings that don't contain any formatted values in an [`FString`].
fn find_useless_f_strings<'a>(
expr: &'a Expr,
locator: &'a Locator,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyflakes/rules/repeated_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub(crate) fn repeated_keys(checker: &mut Checker, keys: &[Option<Expr>], values
};

match key {
Expr::Constant(_) | Expr::Tuple(_) | Expr::JoinedStr(_) => {
Expr::Constant(_) | Expr::Tuple(_) | Expr::FString(_) => {
if checker.enabled(Rule::MultiValueRepeatedKeyLiteral) {
let mut diagnostic = Diagnostic::new(
MultiValueRepeatedKeyLiteral {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) {
}
_ => {}
},
Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Expr::FString(ast::ExprFString { values, range: _ }) => {
checker.diagnostics.push(Diagnostic::new(
AssertOnStringLiteral {
kind: if values.iter().all(|value| match value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn is_valid_default(expr: &Expr) -> bool {
Expr::Constant(ast::ExprConstant {
value: Constant::Str { .. } | Constant::None { .. },
..
}) | Expr::JoinedStr(_)
}) | Expr::FString(_)
)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pylint/rules/invalid_envvar_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn is_valid_key(expr: &Expr) -> bool {
Expr::Constant(ast::ExprConstant {
value: Constant::Str { .. },
..
}) | Expr::JoinedStr(_)
}) | Expr::FString(_)
)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/pylint/rules/single_string_slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn single_string_slots(checker: &mut Checker, class: &StmtClassDef) {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
}) | Expr::JoinedStr(_)
}) | Expr::FString(_)
) {
checker
.diagnostics
Expand All @@ -92,7 +92,7 @@ pub(crate) fn single_string_slots(checker: &mut Checker, class: &StmtClassDef) {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
}) | Expr::JoinedStr(_)
}) | Expr::FString(_)
) {
checker
.diagnostics
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyupgrade/rules/native_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(crate) fn native_literals(
if checker
.semantic()
.current_expressions()
.filter(|expr| expr.is_joined_str_expr())
.filter(|expr| expr.is_f_string_expr())
.count()
> 1
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ pub(crate) fn printf_string_formatting(

// Parse the parameters.
let params_string = match right {
Expr::Constant(_) | Expr::JoinedStr(_) => {
Expr::Constant(_) | Expr::FString(_) => {
format!("({})", checker.locator().slice(right.range()))
}
Expr::Name(_) | Expr::Attribute(_) | Expr::Subscript(_) | Expr::Call(_) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub(crate) fn unnecessary_encode_utf8(checker: &mut Checker, call: &ast::ExprCal
}
}
// Ex) `f"foo{bar}".encode("utf-8")`
Expr::JoinedStr(_) => {
Expr::FString(_) => {
if let Some(encoding_arg) = match_encoding_arg(&call.arguments) {
if let EncodingArg::Keyword(kwarg) = encoding_arg {
// Ex) Convert `f"unicode text©".encode(encoding="utf-8")` to
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/ruff/rules/invalid_index_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn invalid_index_type(checker: &mut Checker, expr: &ExprSubscript) {
Expr::List(_)
| Expr::ListComp(_)
| Expr::Tuple(_)
| Expr::JoinedStr(_)
| Expr::FString(_)
| Expr::Constant(ExprConstant {
value: Constant::Str(_) | Constant::Bytes(_),
..
Expand Down Expand Up @@ -156,7 +156,7 @@ pub(crate) fn invalid_index_type(checker: &mut Checker, expr: &ExprSubscript) {
#[derive(Debug)]
enum CheckableExprType<'a> {
Constant(&'a Constant),
JoinedStr,
FString,
List,
ListComp,
SetComp,
Expand All @@ -171,7 +171,7 @@ impl fmt::Display for CheckableExprType<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Constant(constant) => f.write_str(constant_type_name(constant)),
Self::JoinedStr => f.write_str("str"),
Self::FString => f.write_str("str"),
Self::List => f.write_str("list"),
Self::SetComp => f.write_str("set comprehension"),
Self::ListComp => f.write_str("list comprehension"),
Expand All @@ -188,7 +188,7 @@ impl<'a> CheckableExprType<'a> {
fn try_from(expr: &'a Expr) -> Option<Self> {
match expr {
Expr::Constant(ExprConstant { value, .. }) => Some(Self::Constant(value)),
Expr::JoinedStr(_) => Some(Self::JoinedStr),
Expr::FString(_) => Some(Self::FString),
Expr::List(_) => Some(Self::List),
Expr::ListComp(_) => Some(Self::ListComp),
Expr::SetComp(_) => Some(Self::SetComp),
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/ruff/rules/unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
| Expr::Compare(_)
| Expr::Call(_)
| Expr::FormattedValue(_)
| Expr::JoinedStr(_)
| Expr::FString(_)
| Expr::Constant(_)
| Expr::Attribute(_)
| Expr::Subscript(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
F: (Fn(&str) -> bool) + Copy,
{
match expr {
Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Expr::FString(ast::ExprFString { values, range: _ }) => {
for value in values {
if any_string(value, predicate) {
return true;
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ pub struct ExprFormattedValue<'a> {
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprJoinedStr<'a> {
pub struct ExprFString<'a> {
values: Vec<ComparableExpr<'a>>,
}

Expand Down Expand Up @@ -697,7 +697,7 @@ pub enum ComparableExpr<'a> {
Compare(ExprCompare<'a>),
Call(ExprCall<'a>),
FormattedValue(ExprFormattedValue<'a>),
JoinedStr(ExprJoinedStr<'a>),
FString(ExprFString<'a>),
Constant(ExprConstant<'a>),
Attribute(ExprAttribute<'a>),
Subscript(ExprSubscript<'a>),
Expand Down Expand Up @@ -865,8 +865,8 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
debug_text: debug_text.as_ref(),
format_spec: format_spec.as_ref().map(Into::into),
}),
ast::Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Self::JoinedStr(ExprJoinedStr {
ast::Expr::FString(ast::ExprFString { values, range: _ }) => {
Self::FString(ExprFString {
values: values.iter().map(Into::into).collect(),
})
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
if !matches!(
left.as_ref(),
Expr::Constant(_)
| Expr::JoinedStr(_)
| Expr::FString(_)
| Expr::List(_)
| Expr::Tuple(_)
| Expr::Set(_)
Expand All @@ -82,7 +82,7 @@ where
if !matches!(
right.as_ref(),
Expr::Constant(_)
| Expr::JoinedStr(_)
| Expr::FString(_)
| Expr::List(_)
| Expr::Tuple(_)
| Expr::Set(_)
Expand Down Expand Up @@ -126,7 +126,7 @@ where
Expr::BoolOp(ast::ExprBoolOp {
values, range: _, ..
})
| Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
| Expr::FString(ast::ExprFString { values, range: _ }) => {
values.iter().any(|expr| any_over_expr(expr, func))
}
Expr::NamedExpr(ast::ExprNamedExpr {
Expand Down Expand Up @@ -1094,7 +1094,7 @@ impl Truthiness {
Constant::Complex { real, imag } => Some(*real != 0.0 || *imag != 0.0),
Constant::Ellipsis => Some(true),
},
Expr::JoinedStr(ast::ExprJoinedStr { values, range: _ }) => {
Expr::FString(ast::ExprFString { values, range: _ }) => {
if values.is_empty() {
Some(false)
} else if values.iter().any(|value| {
Expand Down
Loading

0 comments on commit 3f0eea6

Please sign in to comment.