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():...