---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF070 [*] Unnecessary assignment to `x` before `yield` statement
 --> RUF070.py:7:11
  |
5 | def foo():
6 |     x = 1
7 |     yield x  # RUF070
  |           ^
8 |
9 | def foo():
  |
help: Remove unnecessary assignment
  |
5 | def foo():
  -     x = 1
  -     yield x  # RUF070
6 +     yield 1
7 |
  |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield from` statement
  --> RUF070.py:11:16
   |
 9 | def foo():
10 |     x = [1, 2, 3]
11 |     yield from x  # RUF070
   |                ^
12 |
13 | def foo():
   |
help: Remove unnecessary assignment
   |
9  | def foo():
   -     x = [1, 2, 3]
   -     yield from x  # RUF070
10 +     yield from [1, 2, 3]
11 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:16:15
   |
14 |     for i in range(10):
15 |         x = i * 2
16 |         yield x  # RUF070
   |               ^
17 |
18 | def foo():
   |
help: Remove unnecessary assignment
   |
14 |     for i in range(10):
   -         x = i * 2
   -         yield x  # RUF070
15 +         yield i * 2
16 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:21:15
   |
19 |     if True:
20 |         x = 1
21 |         yield x  # RUF070
   |               ^
22 |
23 | def foo():
   |
help: Remove unnecessary assignment
   |
19 |     if True:
   -         x = 1
   -         yield x  # RUF070
20 +         yield 1
21 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:26:15
   |
24 |     with open("foo.txt") as f:
25 |         x = f.read()
26 |         yield x  # RUF070
   |               ^
27 |
28 | def foo():
   |
help: Remove unnecessary assignment
   |
24 |     with open("foo.txt") as f:
   -         x = f.read()
   -         yield x  # RUF070
25 +         yield f.read()
26 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:31:15
   |
29 |     try:
30 |         x = something()
31 |         yield x  # RUF070
   |               ^
32 |     except Exception:
33 |         pass
   |
help: Remove unnecessary assignment
   |
29 |     try:
   -         x = something()
   -         yield x  # RUF070
30 +         yield something()
31 |     except Exception:
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:37:11
   |
35 | def foo():
36 |     x = some_function()
37 |     yield x  # RUF070
   |           ^
38 |
39 | def foo():
   |
help: Remove unnecessary assignment
   |
35 | def foo():
   -     x = some_function()
   -     yield x  # RUF070
36 +     yield some_function()
37 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield from` statement
  --> RUF070.py:41:16
   |
39 | def foo():
40 |     x = some_generator()
41 |     yield from x  # RUF070
   |                ^
42 |
43 | def foo():
   |
help: Remove unnecessary assignment
   |
39 | def foo():
   -     x = some_generator()
   -     yield from x  # RUF070
40 +     yield from some_generator()
41 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:45:11
   |
43 | def foo():
44 |     x = lambda: 1
45 |     yield x  # RUF070
   |           ^
46 |
47 | def foo():
   |
help: Remove unnecessary assignment
   |
43 | def foo():
   -     x = lambda: 1
   -     yield x  # RUF070
44 +     yield lambda: 1
45 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:49:11
   |
47 | def foo():
48 |     x = (y := 1)
49 |     yield x  # RUF070
   |           ^
50 |
51 | def foo():
   |
help: Remove unnecessary assignment
   |
47 | def foo():
   -     x = (y := 1)
   -     yield x  # RUF070
48 +     yield (y := 1)
49 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:53:11
   |
51 | def foo():
52 |     x =1
53 |     yield x  # RUF070 (no space after `=`)
   |           ^
54 |
55 | def foo():
   |
help: Remove unnecessary assignment
   |
51 | def foo():
   -     x =1
   -     yield x  # RUF070 (no space after `=`)
52 +     yield 1
53 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:57:11
   |
55 | def foo():
56 |     x = yield 1
57 |     yield x  # RUF070 (yield as assigned value, fix adds parentheses)
   |           ^
58 |
59 | # Assignment inside `with`, yield outside
   |
help: Remove unnecessary assignment
   |
55 | def foo():
   -     x = yield 1
   -     yield x  # RUF070 (yield as assigned value, fix adds parentheses)
56 +     yield (yield 1)
57 |
   |
note: This is an unsafe fix and may change runtime behavior

RUF070 [*] Unnecessary assignment to `x` before `yield` statement
  --> RUF070.py:63:11
   |
61 |     with open("foo.txt") as f:
62 |         x = f.read()
63 |     yield x  # RUF070
   |           ^
   |
help: Remove unnecessary assignment
   |
61 |     with open("foo.txt") as f:
   -         x = f.read()
   -     yield x  # RUF070
62 +         yield f.read()
63 |
   |
note: This is an unsafe fix and may change runtime behavior
