diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF060.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF060.py index c3b96c6b7be52..78b5888207607 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF060.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF060.py @@ -42,3 +42,7 @@ 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 diff --git a/crates/ruff_linter/src/rules/ruff/rules/in_empty_collection.rs b/crates/ruff_linter/src/rules/ruff/rules/in_empty_collection.rs index 30f4ff8bc71f9..ec3257d9ec5a1 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/in_empty_collection.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/in_empty_collection.rs @@ -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, diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF060_RUF060.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF060_RUF060.py.snap index 20e8cf9f42cac..cd8375a506e7d 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF060_RUF060.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF060_RUF060.py.snap @@ -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 + |