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

Check SIM118 in comprehension #1627

Merged
merged 2 commits into from Jan 4, 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
8 changes: 8 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM118.py
Expand Up @@ -12,3 +12,11 @@
for key in list(obj.keys()):
if some_property(key):
del obj[key]

[k for k in obj.keys()] # SIM118

{k for k in obj.keys()} # SIM118

{k: k for k in obj.keys()} # SIM118

(k for k in obj.keys()) # SIM118
13 changes: 12 additions & 1 deletion src/checkers/ast.rs
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;
use log::error;
use nohash_hasher::IntMap;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_ast::{Located, Location};
use rustpython_ast::{Comprehension, Located, Location};
use rustpython_common::cformat::{CFormatError, CFormatErrorType};
use rustpython_parser::ast::{
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind,
Expand Down Expand Up @@ -2925,6 +2925,17 @@ where
}
}

fn visit_comprehension(&mut self, comprehension: &'b Comprehension) {
if self.settings.enabled.contains(&CheckCode::SIM118) {
flake8_simplify::plugins::key_in_dict_for(
self,
&comprehension.target,
&comprehension.iter,
);
}
visitor::walk_comprehension(self, comprehension);
}

fn visit_arguments(&mut self, arguments: &'b Arguments) {
if self.settings.enabled.contains(&CheckCode::B006) {
flake8_bugbear::plugins::mutable_argument_default(self, arguments);
Expand Down
Expand Up @@ -97,4 +97,80 @@ expression: checks
row: 9
column: 21
parent: ~
- kind:
KeyInDict:
- k
- obj
location:
row: 16
column: 7
end_location:
row: 16
column: 22
fix:
content: obj
location:
row: 16
column: 12
end_location:
row: 16
column: 22
parent: ~
- kind:
KeyInDict:
- k
- obj
location:
row: 18
column: 7
end_location:
row: 18
column: 22
fix:
content: obj
location:
row: 18
column: 12
end_location:
row: 18
column: 22
parent: ~
- kind:
KeyInDict:
- k
- obj
location:
row: 20
column: 10
end_location:
row: 20
column: 25
fix:
content: obj
location:
row: 20
column: 15
end_location:
row: 20
column: 25
parent: ~
- kind:
KeyInDict:
- k
- obj
location:
row: 22
column: 7
end_location:
row: 22
column: 22
fix:
content: obj
location:
row: 22
column: 12
end_location:
row: 22
column: 22
parent: ~