-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-comprehensions
] Detect overshadowed list
/set
/dict
, ig…
…nore variadics and named expressions (`C417`) (#15955) ## Summary Part of #15809 and #15876. This change brings several bugfixes: * The nested `map()` call in `list(map(lambda x: x, []))` where `list` is overshadowed is now correctly reported. * The call will no longer reported if: * Any arguments given to `map()` are variadic. * Any of the iterables contain a named expression. ## Test Plan `cargo nextest run` and `cargo insta test`. --------- Co-authored-by: Micha Reiser <[email protected]>
- Loading branch information
1 parent
349f933
commit 7db5a92
Showing
6 changed files
with
155 additions
and
126 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C417_1.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
##### https://github.com/astral-sh/ruff/issues/15809 | ||
|
||
### Errors | ||
|
||
def overshadowed_list(): | ||
list = ... | ||
list(map(lambda x: x, [])) | ||
|
||
|
||
### No errors | ||
|
||
dict(map(lambda k: (k,), a)) | ||
dict(map(lambda k: (k, v, 0), a)) | ||
dict(map(lambda k: [k], a)) | ||
dict(map(lambda k: [k, v, 0], a)) | ||
dict(map(lambda k: {k, v}, a)) | ||
dict(map(lambda k: {k: 0, v: 1}, a)) | ||
|
||
a = [(1, 2), (3, 4)] | ||
map(lambda x: [*x, 10], *a) | ||
map(lambda x: [*x, 10], *a, *b) | ||
map(lambda x: [*x, 10], a, *b) | ||
|
||
|
||
map(lambda x: x + 10, (a := [])) | ||
list(map(lambda x: x + 10, (a := []))) | ||
set(map(lambda x: x + 10, (a := []))) | ||
dict(map(lambda x: (x, 10), (a := []))) |
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
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
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
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
21 changes: 21 additions & 0 deletions
21
...ehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C417_C417_1.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs | ||
--- | ||
C417_1.py:7:10: C417 [*] Unnecessary `map()` usage (rewrite using a generator expression) | ||
| | ||
5 | def overshadowed_list(): | ||
6 | list = ... | ||
7 | list(map(lambda x: x, [])) | ||
| ^^^^^^^^^^^^^^^^^^^^ C417 | ||
| | ||
= help: Replace `map()` with a generator expression | ||
|
||
ℹ Unsafe fix | ||
4 4 | | ||
5 5 | def overshadowed_list(): | ||
6 6 | list = ... | ||
7 |- list(map(lambda x: x, [])) | ||
7 |+ list((x for x in [])) | ||
8 8 | | ||
9 9 | | ||
10 10 | ### No errors |