---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---
C402 [*] Unnecessary generator (rewrite as a dict comprehension)
 --> C402.py:1:1
  |
1 | dict((x, x) for x in range(3))
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 | dict(
3 |     (x, x) for x in range(3)
  |
help: Rewrite as a dict comprehension
  |
  - dict((x, x) for x in range(3))
1 + {x: x for x in range(3)}
2 | dict(
  |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
 --> C402.py:2:1
  |
1 |   dict((x, x) for x in range(3))
2 | / dict(
3 | |     (x, x) for x in range(3)
4 | | )
  | |_^
5 |   dict(((x, x) for x in range(3)), z=3)
6 |   y = f'{dict((x, x) for x in range(3))}'
  |
help: Rewrite as a dict comprehension
  |
1 | dict((x, x) for x in range(3))
  - dict(
  -     (x, x) for x in range(3)
  - )
2 + {
3 +     x: x for x in range(3)
4 + }
5 | dict(((x, x) for x in range(3)), z=3)
  |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
 --> C402.py:6:8
  |
4 | )
5 | dict(((x, x) for x in range(3)), z=3)
6 | y = f'{dict((x, x) for x in range(3))}'
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 | print(f'Hello {dict((x, x) for x in range(3))} World')
8 | print(f"Hello {dict((x, x) for x in 'abc')} World")
  |
help: Rewrite as a dict comprehension
  |
5 | dict(((x, x) for x in range(3)), z=3)
  - y = f'{dict((x, x) for x in range(3))}'
6 + y = f'{ {x: x for x in range(3)} }'
7 | print(f'Hello {dict((x, x) for x in range(3))} World')
  |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
 --> C402.py:7:16
  |
5 | dict(((x, x) for x in range(3)), z=3)
6 | y = f'{dict((x, x) for x in range(3))}'
7 | print(f'Hello {dict((x, x) for x in range(3))} World')
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8 | print(f"Hello {dict((x, x) for x in 'abc')} World")
9 | print(f'Hello {dict((x, x) for x in "abc")} World')
  |
help: Rewrite as a dict comprehension
  |
6 | y = f'{dict((x, x) for x in range(3))}'
  - print(f'Hello {dict((x, x) for x in range(3))} World')
7 + print(f'Hello { {x: x for x in range(3)} } World')
8 | print(f"Hello {dict((x, x) for x in 'abc')} World")
  |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:8:16
   |
 6 | y = f'{dict((x, x) for x in range(3))}'
 7 | print(f'Hello {dict((x, x) for x in range(3))} World')
 8 | print(f"Hello {dict((x, x) for x in 'abc')} World")
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 9 | print(f'Hello {dict((x, x) for x in "abc")} World')
10 | print(f'Hello {dict((x,x) for x in "abc")} World')
   |
help: Rewrite as a dict comprehension
  |
7 | print(f'Hello {dict((x, x) for x in range(3))} World')
  - print(f"Hello {dict((x, x) for x in 'abc')} World")
8 + print(f"Hello { {x: x for x in 'abc'} } World")
9 | print(f'Hello {dict((x, x) for x in "abc")} World')
  |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:9:16
   |
 7 | print(f'Hello {dict((x, x) for x in range(3))} World')
 8 | print(f"Hello {dict((x, x) for x in 'abc')} World")
 9 | print(f'Hello {dict((x, x) for x in "abc")} World')
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 | print(f'Hello {dict((x,x) for x in "abc")} World')
   |
help: Rewrite as a dict comprehension
   |
8  | print(f"Hello {dict((x, x) for x in 'abc')} World")
   - print(f'Hello {dict((x, x) for x in "abc")} World')
9  + print(f'Hello { {x: x for x in "abc"} } World')
10 | print(f'Hello {dict((x,x) for x in "abc")} World')
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:10:16
   |
 8 | print(f"Hello {dict((x, x) for x in 'abc')} World")
 9 | print(f'Hello {dict((x, x) for x in "abc")} World')
10 | print(f'Hello {dict((x,x) for x in "abc")} World')
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^
11 |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
   |
help: Rewrite as a dict comprehension
   |
9  | print(f'Hello {dict((x, x) for x in "abc")} World')
   - print(f'Hello {dict((x,x) for x in "abc")} World')
10 + print(f'Hello { {x: x for x in "abc"} } World')
11 |
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:12:4
   |
10 | print(f'Hello {dict((x,x) for x in "abc")} World')
11 |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13 | f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
   |
help: Rewrite as a dict comprehension
   |
11 |
   - f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
12 + f'{ {x: x for x in range(3)} | dict((x, x) for x in range(3))}'
13 | f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:12:37
   |
10 | print(f'Hello {dict((x,x) for x in "abc")} World')
11 |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13 | f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
   |
help: Rewrite as a dict comprehension
   |
11 |
   - f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
12 + f'{dict((x, x) for x in range(3)) | {x: x for x in range(3)} }'
13 | f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:13:5
   |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
13 | f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14 |
15 | def f(x):
   |
help: Rewrite as a dict comprehension
   |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
   - f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
13 + f'{ {x: x for x in range(3)} | dict((x, x) for x in range(3)) }'
14 |
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:13:38
   |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
13 | f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
   |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14 |
15 | def f(x):
   |
help: Rewrite as a dict comprehension
   |
12 | f'{dict((x, x) for x in range(3)) | dict((x, x) for x in range(3))}'
   - f'{ dict((x, x) for x in range(3)) | dict((x, x) for x in range(3)) }'
13 + f'{ dict((x, x) for x in range(3)) | {x: x for x in range(3)} }'
14 |
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:18:16
   |
16 |     return x
17 |
18 | print(f'Hello {dict((x,f(x)) for x in "abc")} World')
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19 |
20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
   |
help: Rewrite as a dict comprehension
   |
17 |
   - print(f'Hello {dict((x,f(x)) for x in "abc")} World')
18 + print(f'Hello { {x: f(x) for x in "abc"} } World')
19 |
   |
note: This is an unsafe fix and may change runtime behavior

C402 [*] Unnecessary generator (rewrite as a dict comprehension)
  --> C402.py:21:1
   |
20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
21 | dict((k,v)for k,v in d.iteritems() if k in only_args)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22 |
23 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458940
   |
help: Rewrite as a dict comprehension
   |
20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
   - dict((k,v)for k,v in d.iteritems() if k in only_args)
21 + {k: v for k,v in d.iteritems() if k in only_args}
22 |
   |
note: This is an unsafe fix and may change runtime behavior
