---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---
C401 [*] Unnecessary generator (rewrite as a set comprehension)
 --> C401.py:2:13
  |
1 | # Cannot combine with C416. Should use set comprehension here.
2 | even_nums = set(2 * x for x in range(3))
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 | odd_nums = set(
4 |     2 * x + 1 for x in range(3)
  |
help: Rewrite as a set comprehension
  |
1 | # Cannot combine with C416. Should use set comprehension here.
  - even_nums = set(2 * x for x in range(3))
2 + even_nums = {2 * x for x in range(3)}
3 | odd_nums = set(
  |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
 --> C401.py:3:12
  |
1 |   # Cannot combine with C416. Should use set comprehension here.
2 |   even_nums = set(2 * x for x in range(3))
3 |   odd_nums = set(
  |  ____________^
4 | |     2 * x + 1 for x in range(3)
5 | | )
  | |_^
6 |   small_nums = f"{set(a if a < 6 else 0 for a in range(3))}"
  |
help: Rewrite as a set comprehension
  |
2 | even_nums = set(2 * x for x in range(3))
  - odd_nums = set(
3 + odd_nums = {
4 |     2 * x + 1 for x in range(3)
  - )
5 + }
6 | small_nums = f"{set(a if a < 6 else 0 for a in range(3))}"
  |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
 --> C401.py:6:17
  |
4 |     2 * x + 1 for x in range(3)
5 | )
6 | small_nums = f"{set(a if a < 6 else 0 for a in range(3))}"
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 |
8 | def f(x):
  |
help: Rewrite as a set comprehension
  |
5 | )
  - small_nums = f"{set(a if a < 6 else 0 for a in range(3))}"
6 + small_nums = f"{ {a if a < 6 else 0 for a in range(3)} }"
7 |
  |
note: This is an unsafe fix and may change runtime behavior

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

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

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:16:5
   |
15 | # Short-circuit case, combine with C416 and should produce x = set(range(3))
16 | x = set(x for x in range(3))
   |     ^^^^^^^^^^^^^^^^^^^^^^^^
17 | x = set(
18 |     x for x in range(3)
   |
help: Rewrite using `set()`
   |
15 | # Short-circuit case, combine with C416 and should produce x = set(range(3))
   - x = set(x for x in range(3))
16 + x = set(range(3))
17 | x = set(
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:17:5
   |
15 |   # Short-circuit case, combine with C416 and should produce x = set(range(3))
16 |   x = set(x for x in range(3))
17 |   x = set(
   |  _____^
18 | |     x for x in range(3)
19 | | )
   | |_^
20 |   print(f"Hello {set(a for a in range(3))} World")
21 |   print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |
help: Rewrite using `set()`
   |
16 | x = set(x for x in range(3))
   - x = set(
   -     x for x in range(3)
   - )
17 + x = set(range(3))
18 | print(f"Hello {set(a for a in range(3))} World")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:20:16
   |
18 |     x for x in range(3)
19 | )
20 | print(f"Hello {set(a for a in range(3))} World")
   |                ^^^^^^^^^^^^^^^^^^^^^^^^
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
help: Rewrite using `set()`
   |
19 | )
   - print(f"Hello {set(a for a in range(3))} World")
20 + print(f"Hello {set(range(3))} World")
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:21:10
   |
19 | )
20 | print(f"Hello {set(a for a in range(3))} World")
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |          ^^^^^^^^^^^^^^^^^^^^^
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
help: Rewrite using `set()`
   |
20 | print(f"Hello {set(a for a in range(3))} World")
   - print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
21 + print(f"{set('abc') - set(a for a in 'ab')}")
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:21:34
   |
19 | )
20 | print(f"Hello {set(a for a in range(3))} World")
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |                                  ^^^^^^^^^^^^^^^^^^^^
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
help: Rewrite using `set()`
   |
20 | print(f"Hello {set(a for a in range(3))} World")
   - print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
21 + print(f"{set(a for a in 'abc') - set('ab')}")
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:22:11
   |
20 | print(f"Hello {set(a for a in range(3))} World")
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |           ^^^^^^^^^^^^^^^^^^^^^
23 |
24 | # Strip parentheses from inner generators.
   |
help: Rewrite using `set()`
   |
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
   - print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
22 + print(f"{ set('abc') - set(a for a in 'ab') }")
23 |
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:22:35
   |
20 | print(f"Hello {set(a for a in range(3))} World")
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
22 | print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |                                   ^^^^^^^^^^^^^^^^^^^^
23 |
24 | # Strip parentheses from inner generators.
   |
help: Rewrite using `set()`
   |
21 | print(f"{set(a for a in 'abc') - set(a for a in 'ab')}")
   - print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }")
22 + print(f"{ set(a for a in 'abc') - set('ab') }")
23 |
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:25:1
   |
24 | # Strip parentheses from inner generators.
25 | set((2 * x for x in range(3)))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26 | set(((2 * x for x in range(3))))
27 | set((((2 * x for x in range(3)))))
   |
help: Rewrite as a set comprehension
   |
24 | # Strip parentheses from inner generators.
   - set((2 * x for x in range(3)))
25 + {2 * x for x in range(3)}
26 | set(((2 * x for x in range(3))))
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:26:1
   |
24 | # Strip parentheses from inner generators.
25 | set((2 * x for x in range(3)))
26 | set(((2 * x for x in range(3))))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 | set((((2 * x for x in range(3)))))
   |
help: Rewrite as a set comprehension
   |
25 | set((2 * x for x in range(3)))
   - set(((2 * x for x in range(3))))
26 + {2 * x for x in range(3)}
27 | set((((2 * x for x in range(3)))))
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:27:1
   |
25 | set((2 * x for x in range(3)))
26 | set(((2 * x for x in range(3))))
27 | set((((2 * x for x in range(3)))))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28 |
29 | # Account for trailing comma in fix
   |
help: Rewrite as a set comprehension
   |
26 | set(((2 * x for x in range(3))))
   - set((((2 * x for x in range(3)))))
27 + {2 * x for x in range(3)}
28 |
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:31:1
   |
29 | # Account for trailing comma in fix
30 | # See https://github.com/astral-sh/ruff/issues/15852
31 | set((0 for _ in []),)
   | ^^^^^^^^^^^^^^^^^^^^^
32 | set(
33 |     (0 for _ in [])
   |
help: Rewrite as a set comprehension
   |
30 | # See https://github.com/astral-sh/ruff/issues/15852
   - set((0 for _ in []),)
31 + {0 for _ in []}
32 | set(
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:32:1
   |
30 |   # See https://github.com/astral-sh/ruff/issues/15852
31 |   set((0 for _ in []),)
32 | / set(
33 | |     (0 for _ in [])
34 | |     # some comments
35 | |     ,
36 | |     # some more
37 | | )
   | |_^
38 |
39 |   # t-strings
   |
help: Rewrite as a set comprehension
   |
31 | set((0 for _ in []),)
   - set(
   -     (0 for _ in [])
32 + {
33 +     0 for _ in []
34 |     # some comments
   -     ,
   -     # some more
   - )
35 +     }
36 |
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:40:16
   |
39 | # t-strings
40 | print(t"Hello {set(f(a) for a in 'abc')} World")
   |                ^^^^^^^^^^^^^^^^^^^^^^^^
41 | print(t"Hello { set(f(a) for a in 'abc') } World")
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
   |
help: Rewrite as a set comprehension
   |
39 | # t-strings
   - print(t"Hello {set(f(a) for a in 'abc')} World")
40 + print(t"Hello { {f(a) for a in 'abc'} } World")
41 | print(t"Hello { set(f(a) for a in 'abc') } World")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:41:17
   |
39 | # t-strings
40 | print(t"Hello {set(f(a) for a in 'abc')} World")
41 | print(t"Hello { set(f(a) for a in 'abc') } World")
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
43 | print(t"Hello {set(a for a in range(3))} World")
   |
help: Rewrite as a set comprehension
   |
40 | print(t"Hello {set(f(a) for a in 'abc')} World")
   - print(t"Hello { set(f(a) for a in 'abc') } World")
41 + print(t"Hello { {f(a) for a in 'abc'} } World")
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite as a set comprehension)
  --> C401.py:42:17
   |
40 | print(t"Hello {set(f(a) for a in 'abc')} World")
41 | print(t"Hello { set(f(a) for a in 'abc') } World")
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43 | print(t"Hello {set(a for a in range(3))} World")
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |
help: Rewrite as a set comprehension
   |
41 | print(t"Hello { set(f(a) for a in 'abc') } World")
   - small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
42 + small_nums = t"{ {a if a < 6 else 0 for a in range(3)} }"
43 | print(t"Hello {set(a for a in range(3))} World")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:43:16
   |
41 | print(t"Hello { set(f(a) for a in 'abc') } World")
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
43 | print(t"Hello {set(a for a in range(3))} World")
   |                ^^^^^^^^^^^^^^^^^^^^^^^^
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
help: Rewrite using `set()`
   |
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
   - print(t"Hello {set(a for a in range(3))} World")
43 + print(t"Hello {set(range(3))} World")
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:44:10
   |
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
43 | print(t"Hello {set(a for a in range(3))} World")
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |          ^^^^^^^^^^^^^^^^^^^^^
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
help: Rewrite using `set()`
   |
43 | print(t"Hello {set(a for a in range(3))} World")
   - print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
44 + print(t"{set('abc') - set(a for a in 'ab')}")
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:44:34
   |
42 | small_nums = t"{set(a if a < 6 else 0 for a in range(3))}"
43 | print(t"Hello {set(a for a in range(3))} World")
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
   |                                  ^^^^^^^^^^^^^^^^^^^^
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
help: Rewrite using `set()`
   |
43 | print(t"Hello {set(a for a in range(3))} World")
   - print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
44 + print(t"{set(a for a in 'abc') - set('ab')}")
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:45:11
   |
43 | print(t"Hello {set(a for a in range(3))} World")
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |           ^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite using `set()`
   |
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
   - print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
45 + print(t"{ set('abc') - set(a for a in 'ab') }")
46 |
   |
note: This is an unsafe fix and may change runtime behavior

C401 [*] Unnecessary generator (rewrite using `set()`)
  --> C401.py:45:35
   |
43 | print(t"Hello {set(a for a in range(3))} World")
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
45 | print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
   |                                   ^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite using `set()`
   |
44 | print(t"{set(a for a in 'abc') - set(a for a in 'ab')}")
   - print(t"{ set(a for a in 'abc') - set(a for a in 'ab') }")
45 + print(t"{ set(a for a in 'abc') - set('ab') }")
46 |
   |
note: This is an unsafe fix and may change runtime behavior
