-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8_comprehensions
] Handled special case for C400
which also …
…matches `C416` (#10419) ## Summary Short-circuit implementation mentioned in #10403. I implemented this by extending C400: - Made `UnnecessaryGeneratorList` have information of whether the the short-circuiting occurred (to put diagnostic) - Add additional check for whether in `unnecessary_generator_list` function. Please give me suggestions if you think this isn't the best way to handle this :) ## Test Plan Extended `C400.py` a little, and written the cases where: - Code could be converted to one single conversion to `list` e.g. `list(x for x in range(3))` -> `list(range(3))` - Code couldn't be converted to one single conversion to `list` e.g. `list(2 * x for x in range(3))` -> `[2 * x for x in range(3)]` - `list` function is not built-in, and should not modify the code in any way.
- Loading branch information
1 parent
9675e18
commit 7e652e8
Showing
3 changed files
with
155 additions
and
49 deletions.
There are no files selected for viewing
11 changes: 10 additions & 1 deletion
11
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C400.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
# Cannot combine with C416. Should use list comprehension here. | ||
even_nums = list(2 * x for x in range(3)) | ||
odd_nums = list( | ||
2 * x + 1 for x in range(3) | ||
) | ||
|
||
|
||
# Short-circuit case, combine with C416 and should produce x = list(range(3)) | ||
x = list(x for x in range(3)) | ||
x = list( | ||
x for x in range(3) | ||
) | ||
|
||
|
||
# Not built-in list. | ||
def list(*args, **kwargs): | ||
return None | ||
|
||
|
||
list(2 * x for x in range(3)) | ||
list(x for x in range(3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 72 additions & 24 deletions
96
...prehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C400_C400.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,90 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs | ||
--- | ||
C400.py:1:5: C400 [*] Unnecessary generator (rewrite as a `list` comprehension) | ||
C400.py:2:13: C400 [*] Unnecessary generator (rewrite as a `list` comprehension) | ||
| | ||
1 | x = list(x for x in range(3)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ C400 | ||
2 | x = list( | ||
3 | x for x in range(3) | ||
1 | # Cannot combine with C416. Should use list comprehension here. | ||
2 | even_nums = list(2 * x for x in range(3)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C400 | ||
3 | odd_nums = list( | ||
4 | 2 * x + 1 for x in range(3) | ||
| | ||
= help: Rewrite as a `list` comprehension | ||
|
||
ℹ Unsafe fix | ||
1 |-x = list(x for x in range(3)) | ||
1 |+x = [x for x in range(3)] | ||
2 2 | x = list( | ||
3 3 | x for x in range(3) | ||
4 4 | ) | ||
1 1 | # Cannot combine with C416. Should use list comprehension here. | ||
2 |-even_nums = list(2 * x for x in range(3)) | ||
2 |+even_nums = [2 * x for x in range(3)] | ||
3 3 | odd_nums = list( | ||
4 4 | 2 * x + 1 for x in range(3) | ||
5 5 | ) | ||
|
||
C400.py:2:5: C400 [*] Unnecessary generator (rewrite as a `list` comprehension) | ||
C400.py:3:12: C400 [*] Unnecessary generator (rewrite as a `list` comprehension) | ||
| | ||
1 | x = list(x for x in range(3)) | ||
2 | x = list( | ||
| _____^ | ||
3 | | x for x in range(3) | ||
4 | | ) | ||
1 | # Cannot combine with C416. Should use list comprehension here. | ||
2 | even_nums = list(2 * x for x in range(3)) | ||
3 | odd_nums = list( | ||
| ____________^ | ||
4 | | 2 * x + 1 for x in range(3) | ||
5 | | ) | ||
| |_^ C400 | ||
| | ||
= help: Rewrite as a `list` comprehension | ||
|
||
ℹ Unsafe fix | ||
1 1 | x = list(x for x in range(3)) | ||
2 |-x = list( | ||
2 |+x = [ | ||
3 3 | x for x in range(3) | ||
4 |-) | ||
4 |+] | ||
5 5 | | ||
1 1 | # Cannot combine with C416. Should use list comprehension here. | ||
2 2 | even_nums = list(2 * x for x in range(3)) | ||
3 |-odd_nums = list( | ||
3 |+odd_nums = [ | ||
4 4 | 2 * x + 1 for x in range(3) | ||
5 |-) | ||
5 |+] | ||
6 6 | | ||
7 7 | def list(*args, **kwargs): | ||
7 7 | | ||
8 8 | # Short-circuit case, combine with C416 and should produce x = list(range(3)) | ||
|
||
C400.py:9:5: C400 [*] Unnecessary generator (rewrite using `list()` | ||
| | ||
8 | # Short-circuit case, combine with C416 and should produce x = list(range(3)) | ||
9 | x = list(x for x in range(3)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ C400 | ||
10 | x = list( | ||
11 | x for x in range(3) | ||
| | ||
= help: Rewrite using `list()` | ||
|
||
ℹ Unsafe fix | ||
6 6 | | ||
7 7 | | ||
8 8 | # Short-circuit case, combine with C416 and should produce x = list(range(3)) | ||
9 |-x = list(x for x in range(3)) | ||
9 |+x = list(range(3)) | ||
10 10 | x = list( | ||
11 11 | x for x in range(3) | ||
12 12 | ) | ||
|
||
C400.py:10:5: C400 [*] Unnecessary generator (rewrite using `list()` | ||
| | ||
8 | # Short-circuit case, combine with C416 and should produce x = list(range(3)) | ||
9 | x = list(x for x in range(3)) | ||
10 | x = list( | ||
| _____^ | ||
11 | | x for x in range(3) | ||
12 | | ) | ||
| |_^ C400 | ||
13 | | ||
14 | # Not built-in list. | ||
| | ||
= help: Rewrite using `list()` | ||
|
||
ℹ Unsafe fix | ||
7 7 | | ||
8 8 | # Short-circuit case, combine with C416 and should produce x = list(range(3)) | ||
9 9 | x = list(x for x in range(3)) | ||
10 |-x = list( | ||
11 |- x for x in range(3) | ||
12 |-) | ||
10 |+x = list(range(3)) | ||
13 11 | | ||
14 12 | # Not built-in list. | ||
15 13 | def list(*args, **kwargs): |