From cd338bd768453514412834b7c6d658442770b134 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 20 Sep 2023 17:17:22 -0400 Subject: [PATCH] Add padding in PERF102 fixes --- .../test/fixtures/perflint/PERF102.py | 6 ++++++ .../perflint/rules/incorrect_dict_iterator.rs | 13 ++++++++++-- ...__perflint__tests__PERF102_PERF102.py.snap | 20 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/perflint/PERF102.py b/crates/ruff_linter/resources/test/fixtures/perflint/PERF102.py index c5f7d887d41b9..137ee46693a22 100644 --- a/crates/ruff_linter/resources/test/fixtures/perflint/PERF102.py +++ b/crates/ruff_linter/resources/test/fixtures/perflint/PERF102.py @@ -99,3 +99,9 @@ def f(): def f(): for unused_name, value in some_dict.items(): # PERF102 print(value) + + +# Regression test for: https://github.com/astral-sh/ruff/issues/7097 +def _create_context(name_to_value): + for(B,D)in A.items(): + if(C:=name_to_value.get(B.name)):A.run(B.set,C) diff --git a/crates/ruff_linter/src/rules/perflint/rules/incorrect_dict_iterator.rs b/crates/ruff_linter/src/rules/perflint/rules/incorrect_dict_iterator.rs index 4ad4b24cd086b..ab693beae8b26 100644 --- a/crates/ruff_linter/src/rules/perflint/rules/incorrect_dict_iterator.rs +++ b/crates/ruff_linter/src/rules/perflint/rules/incorrect_dict_iterator.rs @@ -1,5 +1,6 @@ use std::fmt; +use crate::autofix::edits::pad; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast as ast; @@ -102,7 +103,11 @@ pub(crate) fn incorrect_dict_iterator(checker: &mut Checker, stmt_for: &ast::Stm if checker.patch(diagnostic.kind.rule()) { let replace_attribute = Edit::range_replacement("values".to_string(), attr.range()); let replace_target = Edit::range_replacement( - checker.locator().slice(value).to_string(), + pad( + checker.locator().slice(value).to_string(), + stmt_for.target.range(), + checker.locator(), + ), stmt_for.target.range(), ); diagnostic.set_fix(Fix::suggested_edits(replace_attribute, [replace_target])); @@ -120,7 +125,11 @@ pub(crate) fn incorrect_dict_iterator(checker: &mut Checker, stmt_for: &ast::Stm if checker.patch(diagnostic.kind.rule()) { let replace_attribute = Edit::range_replacement("keys".to_string(), attr.range()); let replace_target = Edit::range_replacement( - checker.locator().slice(key).to_string(), + pad( + checker.locator().slice(key).to_string(), + stmt_for.target.range(), + checker.locator(), + ), stmt_for.target.range(), ); diagnostic.set_fix(Fix::suggested_edits(replace_attribute, [replace_target])); diff --git a/crates/ruff_linter/src/rules/perflint/snapshots/ruff_linter__rules__perflint__tests__PERF102_PERF102.py.snap b/crates/ruff_linter/src/rules/perflint/snapshots/ruff_linter__rules__perflint__tests__PERF102_PERF102.py.snap index e5a71ef5ec961..f38200c0dec33 100644 --- a/crates/ruff_linter/src/rules/perflint/snapshots/ruff_linter__rules__perflint__tests__PERF102_PERF102.py.snap +++ b/crates/ruff_linter/src/rules/perflint/snapshots/ruff_linter__rules__perflint__tests__PERF102_PERF102.py.snap @@ -207,5 +207,25 @@ PERF102.py:100:31: PERF102 [*] When using only the values of a dict use the `val 100 |- for unused_name, value in some_dict.items(): # PERF102 100 |+ for value in some_dict.values(): # PERF102 101 101 | print(value) +102 102 | +103 103 | + +PERF102.py:106:16: PERF102 [*] When using only the keys of a dict use the `keys()` method + | +104 | # Regression test for: https://github.com/astral-sh/ruff/issues/7097 +105 | def _create_context(name_to_value): +106 | for(B,D)in A.items(): + | ^^^^^^^ PERF102 +107 | if(C:=name_to_value.get(B.name)):A.run(B.set,C) + | + = help: Replace `.items()` with `.keys()` + +ℹ Suggested fix +103 103 | +104 104 | # Regression test for: https://github.com/astral-sh/ruff/issues/7097 +105 105 | def _create_context(name_to_value): +106 |- for(B,D)in A.items(): + 106 |+ for B in A.keys(): +107 107 | if(C:=name_to_value.get(B.name)):A.run(B.set,C)