Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add padding in PERF102 fixes #7554

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)