Skip to content

Commit

Permalink
Preserve whitespace around ListComp brackets in C419
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Apr 25, 2023
1 parent 4df7bc0 commit b095027
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
all( # first comment
[x.id for x in bar], # second comment
) # third comment
any( # first comment
( # left paren comment
[ # left bracket comment
x.id for x in bar # right bracket comment
] # right paren comment
) # last comment
) # post call comment
any({x.id for x in bar})

# OK
Expand Down
19 changes: 17 additions & 2 deletions crates/ruff/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,26 @@ pub fn fix_unnecessary_comprehension_any_all(
);
};

let mut lpar = list_comp.lpar.clone();
lpar.push(LeftParen {
whitespace_after: list_comp.lbracket.whitespace_after.clone(),
..LeftParen::default()
});

let mut rpar = list_comp.rpar.clone();
rpar.insert(
0,
RightParen {
whitespace_before: list_comp.rbracket.whitespace_before.clone(),
..RightParen::default()
},
);

call.args[0].value = Expression::GeneratorExp(Box::new(GeneratorExp {
elt: list_comp.elt.clone(),
for_in: list_comp.for_in.clone(),
lpar: list_comp.lpar.clone(),
rpar: list_comp.rpar.clone(),
lpar,
rpar,
}));

if let Some(comma) = &call.args[0].comma {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ C419.py:1:5: C419 [*] Unnecessary list comprehension.

Suggested fix
1 |-any([x.id for x in bar])
1 |+any(x.id for x in bar)
1 |+any((x.id for x in bar))
2 2 | all([x.id for x in bar])
3 3 | any( # first comment
4 4 | [x.id for x in bar], # second comment
Expand All @@ -30,7 +30,7 @@ C419.py:2:5: C419 [*] Unnecessary list comprehension.
Suggested fix
1 1 | any([x.id for x in bar])
2 |-all([x.id for x in bar])
2 |+all(x.id for x in bar)
2 |+all((x.id for x in bar))
3 3 | any( # first comment
4 4 | [x.id for x in bar], # second comment
5 5 | ) # third comment
Expand All @@ -51,7 +51,7 @@ C419.py:4:5: C419 [*] Unnecessary list comprehension.
2 2 | all([x.id for x in bar])
3 3 | any( # first comment
4 |- [x.id for x in bar], # second comment
4 |+ x.id for x in bar # second comment
4 |+ (x.id for x in bar) # second comment
5 5 | ) # third comment
6 6 | all( # first comment
7 7 | [x.id for x in bar], # second comment
Expand All @@ -63,7 +63,7 @@ C419.py:7:5: C419 [*] Unnecessary list comprehension.
9 | [x.id for x in bar], # second comment
| ^^^^^^^^^^^^^^^^^^^ C419
10 | ) # third comment
11 | any({x.id for x in bar})
11 | any( # first comment
|
= help: Remove unnecessary list comprehension

Expand All @@ -72,19 +72,46 @@ C419.py:7:5: C419 [*] Unnecessary list comprehension.
5 5 | ) # third comment
6 6 | all( # first comment
7 |- [x.id for x in bar], # second comment
7 |+ x.id for x in bar # second comment
7 |+ (x.id for x in bar) # second comment
8 8 | ) # third comment
9 9 | any({x.id for x in bar})
10 10 |
9 9 | any( # first comment
10 10 | ( # left paren comment

C419.py:9:5: C419 [*] Unnecessary list comprehension.
C419.py:11:9: C419 [*] Unnecessary list comprehension.
|
9 | [x.id for x in bar], # second comment
10 | ) # third comment
11 | any({x.id for x in bar})
11 | any( # first comment
12 | ( # left paren comment
13 | [ # left bracket comment
| _________^
14 | | x.id for x in bar # right bracket comment
15 | | ] # right paren comment
| |_________^ C419
16 | ) # last comment
17 | ) # post call comment
|
= help: Remove unnecessary list comprehension

Suggested fix
8 8 | ) # third comment
9 9 | any( # first comment
10 10 | ( # left paren comment
11 |- [ # left bracket comment
11 |+ ( # left bracket comment
12 12 | x.id for x in bar # right bracket comment
13 |- ] # right paren comment
13 |+ ) # right paren comment
14 14 | ) # last comment
15 15 | ) # post call comment
16 16 | any({x.id for x in bar})

C419.py:16:5: C419 [*] Unnecessary list comprehension.
|
16 | ) # last comment
17 | ) # post call comment
18 | any({x.id for x in bar})
| ^^^^^^^^^^^^^^^^^^^ C419
12 |
13 | # OK
19 |
20 | # OK
|
= help: Remove unnecessary list comprehension

Expand Down

0 comments on commit b095027

Please sign in to comment.