Skip to content

Commit

Permalink
Add padding in PERF102 fixes (#7554)
Browse files Browse the repository at this point in the history
Closes #7097.
  • Loading branch information
charliermarsh authored Sep 20, 2023
1 parent 86faee1 commit 621bed5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]));
Expand All @@ -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]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


0 comments on commit 621bed5

Please sign in to comment.