ruff
ec03eacb - [flake8-bugbear] Fix B023 false positive for immediately-invoked lambdas (#23294)

Commit
3 days ago
[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).
Author
Parents
Loading