mirror of https://github.com/astral-sh/ruff
[`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:
parent
de63f408b9
commit
670fffef37
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
||||
|
|
|
|||
Loading…
Reference in New Issue