Skip to content

Commit 00f702e

Browse files
committed
skip SIM911 when keys/values take arguments
1 parent 2ce3aba commit 00f702e

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM911.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ def foo():
3434
# https://github.com/astral-sh/ruff/issues/18776
3535
flag_stars = {}
3636
for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
37+
38+
# Regression test for https://github.com/astral-sh/ruff/issues/18778
39+
d = {}
40+
for country, stars in zip(d.keys(*x), d.values("hello")):...

crates/ruff_linter/src/rules/flake8_simplify/rules/zip_dict_keys_and_values.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ pub(crate) fn zip_dict_keys_and_values(checker: &Checker, expr: &ast::ExprCall)
7878
let [arg1, arg2] = &args[..] else {
7979
return;
8080
};
81-
let Some((var1, attr1)) = get_var_attr(arg1) else {
81+
let Some((var1, attr1, args1)) = get_var_attr_args(arg1) else {
8282
return;
8383
};
84-
let Some((var2, attr2)) = get_var_attr(arg2) else {
84+
let Some((var2, attr2, args2)) = get_var_attr_args(arg2) else {
8585
return;
8686
};
87-
if var1.id != var2.id || attr1 != "keys" || attr2 != "values" {
87+
if var1.id != var2.id
88+
|| attr1 != "keys"
89+
|| attr2 != "values"
90+
|| !args1.is_empty()
91+
|| !args2.is_empty()
92+
{
8893
return;
8994
}
9095
if !checker.semantic().match_builtin_expr(func, "zip") {
@@ -122,8 +127,11 @@ pub(crate) fn zip_dict_keys_and_values(checker: &Checker, expr: &ast::ExprCall)
122127
)));
123128
}
124129

125-
fn get_var_attr(expr: &Expr) -> Option<(&ExprName, &Identifier)> {
126-
let Expr::Call(ast::ExprCall { func, .. }) = expr else {
130+
fn get_var_attr_args(expr: &Expr) -> Option<(&ExprName, &Identifier, &Arguments)> {
131+
let Expr::Call(ast::ExprCall {
132+
func, arguments, ..
133+
}) = expr
134+
else {
127135
return None;
128136
};
129137
let Expr::Attribute(ExprAttribute { value, attr, .. }) = func.as_ref() else {
@@ -132,5 +140,5 @@ fn get_var_attr(expr: &Expr) -> Option<(&ExprName, &Identifier)> {
132140
let Expr::Name(var_name) = value.as_ref() else {
133141
return None;
134142
};
135-
Some((var_name, attr))
143+
Some((var_name, attr, arguments))
136144
}

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM911_SIM911.py.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ SIM911 [*] Use ` flag_stars.items()` instead of `(zip)(flag_stars.keys(), flag_s
8181
35 | flag_stars = {}
8282
36 | for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
37 |
85+
38 | # Regression test for https://github.com/astral-sh/ruff/issues/18778
8486
|
8587
help: Replace `(zip)(flag_stars.keys(), flag_stars.values())` with ` flag_stars.items()`
8688
33 |
8789
34 | # https://github.com/astral-sh/ruff/issues/18776
8890
35 | flag_stars = {}
8991
- for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
9092
36 + for country, stars in flag_stars.items():...
93+
37 |
94+
38 | # Regression test for https://github.com/astral-sh/ruff/issues/18778
95+
39 | d = {}

0 commit comments

Comments
 (0)