mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 04:29:47 +00:00
[ruff] Detect unnecessary dict comprehensions for iterables (RUF025) (#9613)
## Summary Checks for unnecessary `dict` comprehension when creating a new dictionary from iterable. Suggest to replace with `dict.fromkeys(iterable)` See: https://github.com/astral-sh/ruff/issues/9592 ## Test Plan ```bash cargo test ```
This commit is contained in:
parent
395bf3dc98
commit
eab1a6862b
10 changed files with 488 additions and 3 deletions
92
crates/ruff_linter/resources/test/fixtures/ruff/RUF025.py
vendored
Normal file
92
crates/ruff_linter/resources/test/fixtures/ruff/RUF025.py
vendored
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
# Violation cases: RUF025
|
||||
|
||||
|
||||
def func():
|
||||
numbers = [1, 2, 3]
|
||||
{n: None for n in numbers} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
for key, value in {n: 1 for n in [1, 2, 3]}.items(): # RUF025
|
||||
pass
|
||||
|
||||
|
||||
def func():
|
||||
{n: 1.1 for n in [1, 2, 3]} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{n: complex(3, 5) for n in [1, 2, 3]} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
def f(data):
|
||||
return data
|
||||
|
||||
f({c: "a" for c in "12345"}) # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{n: True for n in [1, 2, 2]} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{n: b"hello" for n in (1, 2, 2)} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{n: ... for n in [1, 2, 3]} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{n: False for n in {1: "a", 2: "b"}} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{(a, b): 1 for (a, b) in [(1, 2), (3, 4)]} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
def f():
|
||||
return 1
|
||||
|
||||
a = f()
|
||||
{n: a for n in [1, 2, 3]} # RUF025
|
||||
|
||||
|
||||
def func():
|
||||
values = ["a", "b", "c"]
|
||||
[{n: values for n in [1, 2, 3]}] # RUF025
|
||||
|
||||
|
||||
# Non-violation cases: RUF025
|
||||
|
||||
|
||||
def func():
|
||||
{n: 1 for n in [1, 2, 3, 4, 5] if n < 3} # OK
|
||||
|
||||
|
||||
def func():
|
||||
{n: 1 for c in [1, 2, 3, 4, 5] for n in [1, 2, 3] if c < 3} # OK
|
||||
|
||||
|
||||
def func():
|
||||
def f():
|
||||
pass
|
||||
|
||||
{n: f() for n in [1, 2, 3]} # OK
|
||||
|
||||
|
||||
def func():
|
||||
{n: n for n in [1, 2, 3, 4, 5]} # OK
|
||||
|
||||
|
||||
def func():
|
||||
def f():
|
||||
return {n: 1 for c in [1, 2, 3, 4, 5] for n in [1, 2, 3]} # OK
|
||||
|
||||
f()
|
||||
|
||||
|
||||
def func():
|
||||
{(a, b): a + b for (a, b) in [(1, 2), (3, 4)]} # OK
|
||||
Loading…
Add table
Add a link
Reference in a new issue