---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B031.py:27:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
25 |     for shopper in shoppers:
26 |         shopper = shopper.title()
27 |         collect_shop_items(shopper, section_items)  # B031
   |                                     ^^^^^^^^^^^^^ B031
28 |     # We're outside the nested loop and used the group again.
29 |     collect_shop_items(shopper, section_items)  # B031
   |

B031.py:29:33: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
27 |         collect_shop_items(shopper, section_items)  # B031
28 |     # We're outside the nested loop and used the group again.
29 |     collect_shop_items(shopper, section_items)  # B031
   |                                 ^^^^^^^^^^^^^ B031
30 |
31 | for _section, section_items in groupby(items, key=lambda p: p[1]):
   |

B031.py:33:31: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
31 | for _section, section_items in groupby(items, key=lambda p: p[1]):
32 |     collect_shop_items("Jane", section_items)
33 |     collect_shop_items("Joe", section_items)  # B031
   |                               ^^^^^^^^^^^^^ B031
   |

B031.py:40:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
38 |     countdown = 3
39 |     while countdown > 0:
40 |         collect_shop_items(shopper, section_items)  # B031
   |                                     ^^^^^^^^^^^^^ B031
41 |         countdown -= 1
   |

B031.py:46:29: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
44 | collection = []
45 | for _section, section_items in groupby(items, key=lambda p: p[1]):
46 |     collection.append([list(section_items) for _ in range(3)])  # B031
   |                             ^^^^^^^^^^^^^ B031
47 |
48 | unique_items = set()
   |

B031.py:56:17: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
55 |     # But it should be detected when used again
56 |     for item in section_items:  # B031
   |                 ^^^^^^^^^^^^^ B031
57 |         another_set.add(item)
   |

B031.py:79:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
77 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
78 |     for shopper in shoppers:
79 |         collect_shop_items(shopper, section_items)  # B031
   |                                     ^^^^^^^^^^^^^ B031
80 |
81 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
   |

B031.py:82:38: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
81 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
82 |     _ = [collect_shop_items(shopper, section_items) for shopper in shoppers]  # B031
   |                                      ^^^^^^^^^^^^^ B031
83 |
84 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
   |

B031.py:94:65: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
   |
92 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
93 |     # The iterator is being used for the second time.
94 |     _ = [(item1, item2) for item1 in section_items for item2 in section_items]  # B031
   |                                                                 ^^^^^^^^^^^^^ B031
95 |
96 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
   |

B031.py:101:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
 99 |     else:
100 |         collect_shop_items(shopper, section_items)
101 |         collect_shop_items(shopper, section_items)  # B031
    |                                     ^^^^^^^^^^^^^ B031
102 |
103 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
    |

B031.py:108:41: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
106 |         collect_shop_items(shopper, section_items)
107 |         if _section == "greens":
108 |             collect_shop_items(shopper, section_items)  # B031
    |                                         ^^^^^^^^^^^^^ B031
109 |         elif _section == "frozen items":
110 |             collect_shop_items(shopper, section_items)  # B031
    |

B031.py:110:41: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
108 |             collect_shop_items(shopper, section_items)  # B031
109 |         elif _section == "frozen items":
110 |             collect_shop_items(shopper, section_items)  # B031
    |                                         ^^^^^^^^^^^^^ B031
111 |         else:
112 |             collect_shop_items(shopper, section_items)  # B031
    |

B031.py:112:41: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
110 |             collect_shop_items(shopper, section_items)  # B031
111 |         else:
112 |             collect_shop_items(shopper, section_items)  # B031
    |                                         ^^^^^^^^^^^^^ B031
113 |         collect_shop_items(shopper, section_items)  # B031
114 |     elif _section == "frozen items":
    |

B031.py:113:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
111 |         else:
112 |             collect_shop_items(shopper, section_items)  # B031
113 |         collect_shop_items(shopper, section_items)  # B031
    |                                     ^^^^^^^^^^^^^ B031
114 |     elif _section == "frozen items":
115 |         # Mix `match` and `if` statements
    |

B031.py:120:49: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
118 |                 collect_shop_items(shopper, section_items)
119 |                 if _section == "fourth":
120 |                     collect_shop_items(shopper, section_items)  # B031
    |                                                 ^^^^^^^^^^^^^ B031
121 |             case _:
122 |                 collect_shop_items(shopper, section_items)
    |

B031.py:126:33: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
124 |         collect_shop_items(shopper, section_items)
125 |     # Now, it should detect
126 |     collect_shop_items(shopper, section_items)  # B031
    |                                 ^^^^^^^^^^^^^ B031
127 |
128 | for _section, section_items in itertools.groupby(items, key=lambda p: p[1]):
    |

B031.py:135:49: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
133 |             match shopper:
134 |                 case "Jane":
135 |                     collect_shop_items(shopper, section_items)  # B031
    |                                                 ^^^^^^^^^^^^^ B031
136 |                 case _:
137 |                     collect_shop_items(shopper, section_items)  # B031
    |

B031.py:137:49: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
135 |                     collect_shop_items(shopper, section_items)  # B031
136 |                 case _:
137 |                     collect_shop_items(shopper, section_items)  # B031
    |                                                 ^^^^^^^^^^^^^ B031
138 |         case "frozen items":
139 |             collect_shop_items(shopper, section_items)
    |

B031.py:140:41: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
138 |         case "frozen items":
139 |             collect_shop_items(shopper, section_items)
140 |             collect_shop_items(shopper, section_items)  # B031
    |                                         ^^^^^^^^^^^^^ B031
141 |         case _:
142 |             collect_shop_items(shopper, section_items)
    |

B031.py:144:33: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
142 |             collect_shop_items(shopper, section_items)
143 |     # Now, it should detect
144 |     collect_shop_items(shopper, section_items)  # B031
    |                                 ^^^^^^^^^^^^^ B031
145 |
146 | for group in groupby(items, key=lambda p: p[1]):
    |

B031.py:200:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
198 |     if _section == "greens":
199 |         collect_shop_items(shopper, section_items)
200 |         collect_shop_items(shopper, section_items)
    |                                     ^^^^^^^^^^^^^ B031
201 |         return
    |

B031.py:210:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
208 |     elif _section == "frozen items":
209 |         collect_shop_items(shopper, section_items)
210 |         collect_shop_items(shopper, section_items)
    |                                     ^^^^^^^^^^^^^ B031
211 |
212 | # Should trigger, since only one branch has a return statement.
    |

B031.py:219:33: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
    |
217 |     elif _section == "frozen items":
218 |         collect_shop_items(shopper, section_items)
219 |     collect_shop_items(shopper, section_items)  # B031
    |                                 ^^^^^^^^^^^^^ B031
220 |
221 | # Let's redefine the `groupby` function to make sure we pick up the correct one.
    |
