[`ruff`] Use helper function for empty f-string detection in `in-empty-collection` (`RUF060`) (#20249)

## Summary

Fixes #20238

Replace inline f-string emptiness check with `is_empty_f_string` helper
function
This commit is contained in:
Takayuki Maeda 2025-09-05 05:20:59 +09:00 committed by GitHub
parent de63f408b9
commit 670fffef37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 5 deletions

View File

@ -42,3 +42,7 @@ b"a" in bytes("a", "utf-8")
1 in set(set([1]))
'' in {""}
frozenset() in {frozenset()}
# https://github.com/astral-sh/ruff/issues/20238
"b" in f"" "" # Error
"b" in f"" "x" # OK

View File

@ -1,5 +1,5 @@
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::{self as ast, CmpOp, Expr};
use ruff_python_ast::{self as ast, CmpOp, Expr, helpers::is_empty_f_string};
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
@ -75,10 +75,7 @@ fn is_empty(expr: &Expr, semantic: &SemanticModel) -> bool {
Expr::Dict(ast::ExprDict { items, .. }) => items.is_empty(),
Expr::BytesLiteral(ast::ExprBytesLiteral { value, .. }) => value.is_empty(),
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => value.is_empty(),
Expr::FString(s) => s
.value
.elements()
.all(|elt| elt.as_literal().is_some_and(|elt| elt.is_empty())),
Expr::FString(s) => is_empty_f_string(s),
Expr::Call(ast::ExprCall {
func,
arguments,

View File

@ -251,3 +251,12 @@ RUF060 Unnecessary membership test on empty collection
25 |
26 | # OK
|
RUF060 Unnecessary membership test on empty collection
--> RUF060.py:47:1
|
46 | # https://github.com/astral-sh/ruff/issues/20238
47 | "b" in f"" "" # Error
| ^^^^^^^^^^^^^
48 | "b" in f"" "x" # OK
|