Commit ec03eac
authored
[flake8-bugbear] Fix B023 false positive for immediately-invoked lambdas (#23294)
## Summary
Fixes #7847.
B023 (`function-uses-loop-variable`) currently flags lambdas that
reference loop variables even when the lambda is immediately invoked
(IIFE pattern). Since the closure is consumed right away, late-binding
is not a concern and the diagnostic is a false positive.
This PR marks immediately-invoked lambdas as safe by checking
`func.is_lambda_expr()` at the call site visitor, pushing the lambda
into `safe_functions` so its body is not flagged.
## Test Plan
Added test cases to `B023.py` covering several IIFE patterns:
```python
for i in range(3):
(lambda: i)() # OK — immediately invoked
(lambda x=i: x)() # OK — default arg + immediately invoked
print((lambda: i)()) # OK — nested in another call
result = (lambda i=i: i * 2)() # OK — default arg shadows
```
All existing B023 tests continue to pass. No snapshot changes needed
(the new cases produce no diagnostics, as expected).1 parent be422f6 commit ec03eac
2 files changed
Lines changed: 15 additions & 0 deletions
File tree
- crates/ruff_linter
- resources/test/fixtures/flake8_bugbear
- src/rules/flake8_bugbear/rules
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
135 | 141 | | |
136 | 142 | | |
137 | 143 | | |
| |||
0 commit comments