diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF037.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF037.py index 75818e741b..e0460df98c 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF037.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF037.py @@ -107,3 +107,6 @@ deque(f"{x}" "") # OK deque(t"") deque(t"" t"") deque(t"{""}") # OK + +# https://github.com/astral-sh/ruff/issues/20050 +deque(f"{""}") # RUF037 diff --git a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_literal_within_deque_call.rs b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_literal_within_deque_call.rs index 88374119c8..0eb3640ad8 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_literal_within_deque_call.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_literal_within_deque_call.rs @@ -1,5 +1,7 @@ use ruff_diagnostics::{Applicability, Edit}; use ruff_macros::{ViolationMetadata, derive_message_formats}; + +use ruff_python_ast::helpers::is_empty_f_string; use ruff_python_ast::parenthesize::parenthesized_range; use ruff_python_ast::{self as ast, Expr}; use ruff_text_size::Ranged; @@ -102,7 +104,7 @@ pub(crate) fn unnecessary_literal_within_deque_call(checker: &Checker, deque: &a } Expr::StringLiteral(string) => string.value.is_empty(), Expr::BytesLiteral(bytes) => bytes.value.is_empty(), - Expr::FString(fstring) => fstring.value.is_empty_literal(), + Expr::FString(fstring) => is_empty_f_string(fstring), Expr::TString(tstring) => tstring.value.is_empty_iterable(), _ => false, }; diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF037_RUF037.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF037_RUF037.py.snap index 8950219905..7334701e64 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF037_RUF037.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF037_RUF037.py.snap @@ -369,6 +369,7 @@ help: Replace with `deque()` 107 + deque() 108 | deque(t"" t"") 109 | deque(t"{""}") # OK +110 | RUF037 [*] Unnecessary empty iterable within a deque call --> RUF037.py:108:1 @@ -386,3 +387,19 @@ help: Replace with `deque()` - deque(t"" t"") 108 + deque() 109 | deque(t"{""}") # OK +110 | +111 | # https://github.com/astral-sh/ruff/issues/20050 + +RUF037 [*] Unnecessary empty iterable within a deque call + --> RUF037.py:112:1 + | +111 | # https://github.com/astral-sh/ruff/issues/20050 +112 | deque(f"{""}") # RUF037 + | ^^^^^^^^^^^^^^ + | +help: Replace with `deque()` +109 | deque(t"{""}") # OK +110 | +111 | # https://github.com/astral-sh/ruff/issues/20050 + - deque(f"{""}") # RUF037 +112 + deque() # RUF037 diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index bc1195d347..4390659858 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -1406,7 +1406,7 @@ fn is_non_empty_f_string(expr: &ast::ExprFString) -> bool { /// Returns `true` if the expression definitely resolves to the empty string, when used as an f-string /// expression. -fn is_empty_f_string(expr: &ast::ExprFString) -> bool { +pub fn is_empty_f_string(expr: &ast::ExprFString) -> bool { fn inner(expr: &Expr) -> bool { match expr { Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => value.is_empty(),