---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B007.py:6:5: B007 [*] Loop control variable `i` not used within loop body
  |
6 | print(i)  # name no longer defined on Python 3; no warning yet
7 | 
8 | for i in range(10):  # name not used within the loop; B007
  |     ^ B007
9 |     print(10)
  |
  = help: Rename unused `i` to `_i`

B007.py:18:13: B007 [*] Loop control variable `k` not used within loop body
   |
18 | for i in range(10):
19 |     for j in range(10):
20 |         for k in range(10):  # k not used, i and j used transitively
   |             ^ B007
21 |             print(i + j)
   |
   = help: Rename unused `k` to `_k`

ℹ Suggested fix
15 15 | 
16 16 | for i in range(10):
17 17 |     for j in range(10):
18    |-        for k in range(10):  # k not used, i and j used transitively
   18 |+        for _k in range(10):  # k not used, i and j used transitively
19 19 |             print(i + j)
20 20 | 
21 21 | 

B007.py:30:5: B007 [*] Loop control variable `i` not used within loop body
   |
30 | for i, (j, (k, l)) in strange_generator():  # i, k not used
   |     ^ B007
31 |     print(j, l)
   |
   = help: Rename unused `i` to `_i`

B007.py:30:13: B007 [*] Loop control variable `k` not used within loop body
   |
30 | for i, (j, (k, l)) in strange_generator():  # i, k not used
   |             ^ B007
31 |     print(j, l)
   |
   = help: Rename unused `k` to `_k`

ℹ Suggested fix
27 27 |                     yield i, (j, (k, l))
28 28 | 
29 29 | 
30    |-for i, (j, (k, l)) in strange_generator():  # i, k not used
   30 |+for i, (j, (_k, l)) in strange_generator():  # i, k not used
31 31 |     print(j, l)
32 32 | 
33 33 | FMT = "{foo} {bar}"

B007.py:34:10: B007 Loop control variable `bar` may not be used within loop body
   |
34 | FMT = "{foo} {bar}"
35 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
36 |     if foo:
37 |         print(FMT.format(**locals()))
   |

B007.py:38:10: B007 Loop control variable `bar` may not be used within loop body
   |
38 |         print(FMT.format(**locals()))
39 | 
40 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
41 |     if foo:
42 |         print(FMT.format(**globals()))
   |

B007.py:42:10: B007 Loop control variable `bar` may not be used within loop body
   |
42 |         print(FMT.format(**globals()))
43 | 
44 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
45 |     if foo:
46 |         print(FMT.format(**vars()))
   |

B007.py:46:10: B007 Loop control variable `bar` may not be used within loop body
   |
46 |         print(FMT.format(**vars()))
47 | 
48 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
49 |     print(FMT.format(foo=foo, bar=eval("bar")))
   |

B007.py:52:14: B007 [*] Loop control variable `bar` not used within loop body
   |
52 | def f():
53 |     # Fixable.
54 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
55 |         if foo or baz:
56 |             break
   |
   = help: Rename unused `bar` to `_bar`

ℹ Suggested fix
49 49 | 
50 50 | def f():
51 51 |     # Fixable.
52    |-    for foo, bar, baz in (["1", "2", "3"],):
   52 |+    for foo, _bar, baz in (["1", "2", "3"],):
53 53 |         if foo or baz:
54 54 |             break
55 55 | 

B007.py:59:14: B007 [*] Loop control variable `bar` not used within loop body
   |
59 | def f():
60 |     # Unfixable due to usage of `bar` outside of loop.
61 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
62 |         if foo or baz:
63 |             break
   |
   = help: Rename unused `bar` to `_bar`

B007.py:68:14: B007 [*] Loop control variable `bar` not used within loop body
   |
68 | def f():
69 |     # Fixable.
70 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
71 |         if foo or baz:
72 |             break
   |
   = help: Rename unused `bar` to `_bar`

ℹ Suggested fix
65 65 | 
66 66 | def f():
67 67 |     # Fixable.
68    |-    for foo, bar, baz in (["1", "2", "3"],):
   68 |+    for foo, _bar, baz in (["1", "2", "3"],):
69 69 |         if foo or baz:
70 70 |             break
71 71 | 

B007.py:77:14: B007 [*] Loop control variable `bar` not used within loop body
   |
77 | def f():
78 |     # Fixable.
79 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
80 |         if foo or baz:
81 |             break
   |
   = help: Rename unused `bar` to `_bar`

ℹ Suggested fix
74 74 | 
75 75 | def f():
76 76 |     # Fixable.
77    |-    for foo, bar, baz in (["1", "2", "3"],):
   77 |+    for foo, _bar, baz in (["1", "2", "3"],):
78 78 |         if foo or baz:
79 79 |             break
80 80 | 

B007.py:87:5: B007 Loop control variable `line_` not used within loop body
   |
87 | # Unfixable due to trailing underscore (`_line_` wouldn't be considered an ignorable
88 | # variable name).
89 | for line_ in range(self.header_lines):
   |     ^^^^^ B007
90 |      fp.readline()
   |


