---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:12:5
   |
10 | for elem in some_list:
11 |     # errors
12 |     some_list.remove(0)
   |     ^^^^^^^^^^^^^^^^
13 |     del some_list[2]
14 |     some_list.append(elem)
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:13:5
   |
11 |     # errors
12 |     some_list.remove(0)
13 |     del some_list[2]
   |     ^^^^^^^^^^^^^^^^
14 |     some_list.append(elem)
15 |     some_list.sort()
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:14:5
   |
12 |     some_list.remove(0)
13 |     del some_list[2]
14 |     some_list.append(elem)
   |     ^^^^^^^^^^^^^^^^
15 |     some_list.sort()
16 |     some_list.reverse()
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:15:5
   |
13 |     del some_list[2]
14 |     some_list.append(elem)
15 |     some_list.sort()
   |     ^^^^^^^^^^^^^^
16 |     some_list.reverse()
17 |     some_list.clear()
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:16:5
   |
14 |     some_list.append(elem)
15 |     some_list.sort()
16 |     some_list.reverse()
   |     ^^^^^^^^^^^^^^^^^
17 |     some_list.clear()
18 |     some_list.extend([1, 2])
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:17:5
   |
15 |     some_list.sort()
16 |     some_list.reverse()
17 |     some_list.clear()
   |     ^^^^^^^^^^^^^^^
18 |     some_list.extend([1, 2])
19 |     some_list.insert(1, 1)
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:18:5
   |
16 |     some_list.reverse()
17 |     some_list.clear()
18 |     some_list.extend([1, 2])
   |     ^^^^^^^^^^^^^^^^
19 |     some_list.insert(1, 1)
20 |     some_list.pop(1)
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:19:5
   |
17 |     some_list.clear()
18 |     some_list.extend([1, 2])
19 |     some_list.insert(1, 1)
   |     ^^^^^^^^^^^^^^^^
20 |     some_list.pop(1)
21 |     some_list.pop()
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:20:5
   |
18 |     some_list.extend([1, 2])
19 |     some_list.insert(1, 1)
20 |     some_list.pop(1)
   |     ^^^^^^^^^^^^^
21 |     some_list.pop()
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:21:5
   |
19 |     some_list.insert(1, 1)
20 |     some_list.pop(1)
21 |     some_list.pop()
   |     ^^^^^^^^^^^^^
22 |
23 |     # conditional break should error
   |

B909 Mutation to loop iterable `some_list` during iteration
  --> B909.py:25:9
   |
23 |     # conditional break should error
24 |     if elem == 2:
25 |         some_list.remove(0)
   |         ^^^^^^^^^^^^^^^^
26 |         if elem == 3:
27 |             break
   |

B909 Mutation to loop iterable `mydicts` during iteration
  --> B909.py:47:5
   |
45 | for elem in mydicts:
46 |     # errors
47 |     mydicts.popitem()
   |     ^^^^^^^^^^^^^^^
48 |     mydicts.setdefault("foo", 1)
49 |     mydicts.update({"foo": "bar"})
   |

B909 Mutation to loop iterable `mydicts` during iteration
  --> B909.py:48:5
   |
46 |     # errors
47 |     mydicts.popitem()
48 |     mydicts.setdefault("foo", 1)
   |     ^^^^^^^^^^^^^^^^^^
49 |     mydicts.update({"foo": "bar"})
   |

B909 Mutation to loop iterable `mydicts` during iteration
  --> B909.py:49:5
   |
47 |     mydicts.popitem()
48 |     mydicts.setdefault("foo", 1)
49 |     mydicts.update({"foo": "bar"})
   |     ^^^^^^^^^^^^^^
50 |
51 |     # no errors
   |

B909 Mutation to loop iterable `myset` during iteration
  --> B909.py:62:5
   |
60 | for _ in myset:
61 |     # errors
62 |     myset.update({4, 5})
   |     ^^^^^^^^^^^^
63 |     myset.intersection_update({4, 5})
64 |     myset.difference_update({4, 5})
   |

B909 Mutation to loop iterable `myset` during iteration
  --> B909.py:63:5
   |
61 |     # errors
62 |     myset.update({4, 5})
63 |     myset.intersection_update({4, 5})
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
64 |     myset.difference_update({4, 5})
65 |     myset.symmetric_difference_update({4, 5})
   |

B909 Mutation to loop iterable `myset` during iteration
  --> B909.py:64:5
   |
62 |     myset.update({4, 5})
63 |     myset.intersection_update({4, 5})
64 |     myset.difference_update({4, 5})
   |     ^^^^^^^^^^^^^^^^^^^^^^^
65 |     myset.symmetric_difference_update({4, 5})
66 |     myset.add(4)
   |

B909 Mutation to loop iterable `myset` during iteration
  --> B909.py:65:5
   |
63 |     myset.intersection_update({4, 5})
64 |     myset.difference_update({4, 5})
65 |     myset.symmetric_difference_update({4, 5})
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66 |     myset.add(4)
67 |     myset.discard(3)
   |

B909 Mutation to loop iterable `myset` during iteration
  --> B909.py:66:5
   |
64 |     myset.difference_update({4, 5})
65 |     myset.symmetric_difference_update({4, 5})
66 |     myset.add(4)
   |     ^^^^^^^^^
67 |     myset.discard(3)
   |

B909 Mutation to loop iterable `myset` during iteration
  --> B909.py:67:5
   |
65 |     myset.symmetric_difference_update({4, 5})
66 |     myset.add(4)
67 |     myset.discard(3)
   |     ^^^^^^^^^^^^^
68 |
69 |     # no errors
   |

B909 Mutation to loop iterable `a.some_list` during iteration
  --> B909.py:84:5
   |
82 | # ensure member accesses are handled as errors
83 | for elem in a.some_list:
84 |     a.some_list.remove(0)
   |     ^^^^^^^^^^^^^^^^^^
85 |     del a.some_list[2]
   |

B909 Mutation to loop iterable `a.some_list` during iteration
  --> B909.py:85:5
   |
83 | for elem in a.some_list:
84 |     a.some_list.remove(0)
85 |     del a.some_list[2]
   |     ^^^^^^^^^^^^^^^^^^
   |

B909 Mutation to loop iterable `foo` during iteration
  --> B909.py:93:5
   |
91 | bar = [4, 5, 6]
92 | for _ in foo:
93 |     foo *= 2
   |     ^^^^^^^^
94 |     foo += bar
95 |     foo[1] = 9
   |

B909 Mutation to loop iterable `foo` during iteration
  --> B909.py:94:5
   |
92 | for _ in foo:
93 |     foo *= 2
94 |     foo += bar
   |     ^^^^^^^^^^
95 |     foo[1] = 9
96 |     foo[1:2] = bar
   |

B909 Mutation to loop iterable `foo` during iteration
  --> B909.py:95:5
   |
93 |     foo *= 2
94 |     foo += bar
95 |     foo[1] = 9
   |     ^^^^^^^^^^
96 |     foo[1:2] = bar
97 |     foo[1:2:3] = bar
   |

B909 Mutation to loop iterable `foo` during iteration
  --> B909.py:96:5
   |
94 |     foo += bar
95 |     foo[1] = 9
96 |     foo[1:2] = bar
   |     ^^^^^^^^^^^^^^
97 |     foo[1:2:3] = bar
   |

B909 Mutation to loop iterable `foo` during iteration
  --> B909.py:97:5
   |
95 |     foo[1] = 9
96 |     foo[1:2] = bar
97 |     foo[1:2:3] = bar
   |     ^^^^^^^^^^^^^^^^
98 |
99 | foo = {1, 2, 3}
   |

B909 Mutation to loop iterable `foo` during iteration
   --> B909.py:102:5
    |
100 | bar = {4, 5, 6}
101 | for _ in foo:  # should error
102 |     foo |= bar
    |     ^^^^^^^^^^
103 |     foo &= bar
104 |     foo -= bar
    |

B909 Mutation to loop iterable `foo` during iteration
   --> B909.py:103:5
    |
101 | for _ in foo:  # should error
102 |     foo |= bar
103 |     foo &= bar
    |     ^^^^^^^^^^
104 |     foo -= bar
105 |     foo ^= bar
    |

B909 Mutation to loop iterable `foo` during iteration
   --> B909.py:104:5
    |
102 |     foo |= bar
103 |     foo &= bar
104 |     foo -= bar
    |     ^^^^^^^^^^
105 |     foo ^= bar
    |

B909 Mutation to loop iterable `foo` during iteration
   --> B909.py:105:5
    |
103 |     foo &= bar
104 |     foo -= bar
105 |     foo ^= bar
    |     ^^^^^^^^^^
    |

B909 Mutation to loop iterable `foo` during iteration
   --> B909.py:136:9
    |
134 |         pass
135 |     else:
136 |         foo.remove(1)
    |         ^^^^^^^^^^
137 |
138 | # should error
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:140:8
    |
138 | # should error
139 | for elem in some_list:
140 |     if some_list.pop() == 2:
    |        ^^^^^^^^^^^^^
141 |         pass
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:150:8
    |
148 | # should error
149 | for elem in some_list:
150 |     if some_list.pop() == 2:
    |        ^^^^^^^^^^^^^
151 |         pass
152 |     else:
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:157:5
    |
155 | # should error
156 | for elem in some_list:
157 |     del some_list[elem]
    |     ^^^^^^^^^^^^^^^^^^^
158 |     some_list.remove(elem)
159 |     some_list.discard(elem)
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:158:5
    |
156 | for elem in some_list:
157 |     del some_list[elem]
158 |     some_list.remove(elem)
    |     ^^^^^^^^^^^^^^^^
159 |     some_list.discard(elem)
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:159:5
    |
157 |     del some_list[elem]
158 |     some_list.remove(elem)
159 |     some_list.discard(elem)
    |     ^^^^^^^^^^^^^^^^^
160 |
161 | # should not error
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:167:5
    |
165 | # should error
166 | for i, elem in enumerate(some_list):
167 |     some_list.pop(0)
    |     ^^^^^^^^^^^^^
168 |
169 | # should not error (list)
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:206:5
    |
204 | # should error - continue followed by unreachable break (issue #12402)
205 | for item in some_list:
206 |     some_list.remove(item)
    |     ^^^^^^^^^^^^^^^^
207 |     continue
208 |     break
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:212:5
    |
210 | # should error - nested `break` does not exit the outer loop
211 | for item in some_list:
212 |     some_list.append(item)
    |     ^^^^^^^^^^^^^^^^
213 |     for _ in range(3):
214 |         break
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:218:5
    |
216 | # should error - nested `continue` does not exit the outer loop
217 | for item in some_list:
218 |     some_list.append(item)
    |     ^^^^^^^^^^^^^^^^
219 |     for _ in range(3):
220 |         continue
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:224:5
    |
222 | # should error - nested `while`'s `break` only exits the inner loop
223 | for item in some_list:
224 |     some_list.append(item)
    |     ^^^^^^^^^^^^^^^^
225 |     while True:
226 |         break
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:232:9
    |
230 | def fail_nested_for_else(some_list, other):
231 |     for item in some_list:
232 |         some_list.append(item)
    |         ^^^^^^^^^^^^^^^^
233 |         for y in other:
234 |             if y:
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:243:9
    |
241 | def fail_nested_while_else(some_list):
242 |     for item in some_list:
243 |         some_list.append(item)
    |         ^^^^^^^^^^^^^^^^
244 |         while True:
245 |             break
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:253:13
    |
251 |     for item in some_list:
252 |         try:
253 |             some_list.append(item)
    |             ^^^^^^^^^^^^^^^^
254 |         except Exception:
255 |             return
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:265:13
    |
263 |             return
264 |         else:
265 |             some_list.append(item)
    |             ^^^^^^^^^^^^^^^^
266 |
267 | # should error - `return` in `except` must not clear mutation in `finally`
    |

B909 Mutation to loop iterable `some_list` during iteration
   --> B909.py:275:13
    |
273 |             return
274 |         finally:
275 |             some_list.append(item)
    |             ^^^^^^^^^^^^^^^^
276 |
277 | # should not error - `finally: return` unconditionally exits, so the `try`
    |

B909 Mutation to loop iterable `outer` during iteration
   --> B909.py:290:9
    |
288 | def fail_nested_return_bypasses_else(outer, inner):
289 |     for item in outer:
290 |         outer.append(item)
    |         ^^^^^^^^^^^^
291 |         for y in inner:
292 |             if y:
    |

B909 Mutation to loop iterable `outer` during iteration
   --> B909.py:300:9
    |
298 | def fail_nested_raise_bypasses_else(outer, inner):
299 |     for item in outer:
300 |         outer.append(item)
    |         ^^^^^^^^^^^^
301 |         for y in inner:
302 |             if y:
    |
