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

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

ℹ Unsafe 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`

ℹ Unsafe 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
   |
33 | FMT = "{foo} {bar}"
34 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
35 |     if foo:
36 |         print(FMT.format(**locals()))
   |
   = help: Rename unused `bar` to `_bar`

B007.py:38:10: B007 Loop control variable `bar` may not be used within loop body
   |
36 |         print(FMT.format(**locals()))
37 | 
38 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
39 |     if foo:
40 |         print(FMT.format(**globals()))
   |
   = help: Rename unused `bar` to `_bar`

B007.py:42:10: B007 Loop control variable `bar` may not be used within loop body
   |
40 |         print(FMT.format(**globals()))
41 | 
42 | for foo, bar in [(1, 2)]:
   |          ^^^ B007
43 |     if foo:
44 |         print(FMT.format(**vars()))
   |
   = help: Rename unused `bar` to `_bar`

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

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

ℹ Unsafe 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
   |
57 | def f():
58 |     # Unfixable due to usage of `bar` outside of loop.
59 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
60 |         if foo or baz:
61 |             break
   |
   = help: Rename unused `bar` to `_bar`

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

ℹ Unsafe 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
   |
75 | def f():
76 |     # Unfixable.
77 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
78 |         if foo or baz:
79 |             break
   |
   = help: Rename unused `bar` to `_bar`

B007.py:88:14: B007 Loop control variable `bar` not used within loop body
   |
86 | def f():
87 |     # Unfixable (false negative) due to usage of `bar` outside of loop.
88 |     for foo, bar, baz in (["1", "2", "3"],):
   |              ^^^ B007
89 |         if foo or baz:
90 |             break
   |
   = help: Rename unused `bar` to `_bar`

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


