Skip to content

Commit

Permalink
Check SIM118 in comprehension (#1627)
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy committed Jan 4, 2023
1 parent 4473c7e commit 0a0e192
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
8 changes: 8 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM118.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: ~

0 comments on commit 0a0e192

Please sign in to comment.