From 9e9c4fe17b4babb2556af93b6a99601331722cf3 Mon Sep 17 00:00:00 2001 From: Victor Hugo Gomes Date: Mon, 23 Jun 2025 11:24:47 -0300 Subject: [PATCH] [`flake8-simplify`] Fix `SIM911` autofix creating a syntax error (#18793) ## Summary The fix would create a syntax error if there wasn't a space between the `in` keyword and the following expression. For example: ```python for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):... ``` I also noticed that the tests for `SIM911` were note being run, so I fixed that. Fixes #18776 ## Test Plan Add regression test --- .../test/fixtures/flake8_simplify/SIM911.py | 5 +++++ .../rules/zip_dict_keys_and_values.rs | 7 ++++++- ...ake8_simplify__tests__SIM911_SIM911.py.snap | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM911.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM911.py index 87ba968868..3dd52b4139 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM911.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM911.py @@ -29,3 +29,8 @@ def foo(): dict = {} for country, stars in zip(dict.keys(), dict.values()): ... + + +# https://github.com/astral-sh/ruff/issues/18776 +flag_stars = {} +for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):... diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/zip_dict_keys_and_values.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/zip_dict_keys_and_values.rs index 1eff193e3c..58f3a01a8a 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/zip_dict_keys_and_values.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/zip_dict_keys_and_values.rs @@ -4,6 +4,7 @@ use ruff_python_ast::{self as ast, Arguments, Expr}; use ruff_python_semantic::analyze::typing::is_dict; use ruff_text_size::Ranged; +use crate::fix::edits; use crate::{AlwaysFixableViolation, Edit, Fix}; use crate::{checkers::ast::Checker, fix::snippet::SourceCodeSnippet}; @@ -101,7 +102,11 @@ pub(crate) fn zip_dict_keys_and_values(checker: &Checker, expr: &ast::ExprCall) return; } - let expected = format!("{}.items()", checker.locator().slice(var1)); + let expected = edits::pad( + format!("{}.items()", checker.locator().slice(var1)), + expr.range(), + checker.locator(), + ); let actual = checker.locator().slice(expr); let mut diagnostic = checker.report_diagnostic( diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM911_SIM911.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM911_SIM911.py.snap index dd92e6fa83..261332a8be 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM911_SIM911.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM911_SIM911.py.snap @@ -75,3 +75,21 @@ SIM911.py:30:27: SIM911 [*] Use `dict.items()` instead of `zip(dict.keys(), dict 30 |- for country, stars in zip(dict.keys(), dict.values()): 30 |+ for country, stars in dict.items(): 31 31 | ... +32 32 | +33 33 | + +SIM911.py:36:22: SIM911 [*] Use ` flag_stars.items()` instead of `(zip)(flag_stars.keys(), flag_stars.values())` + | +34 | # https://github.com/astral-sh/ruff/issues/18776 +35 | flag_stars = {} +36 | for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):... + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM911 + | + = help: Replace `(zip)(flag_stars.keys(), flag_stars.values())` with ` flag_stars.items()` + +ℹ Safe fix +33 33 | +34 34 | # https://github.com/astral-sh/ruff/issues/18776 +35 35 | flag_stars = {} +36 |-for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):... + 36 |+for country, stars in flag_stars.items():...