Fix FURB108 false positive when operands depend on short-circuit evaluation#364
Closed
worksbyfriday wants to merge 1 commit into
Closed
Fix FURB108 false positive when operands depend on short-circuit evaluation#364worksbyfriday wants to merge 1 commit into
worksbyfriday wants to merge 1 commit into
Conversation
…valuation When comparing values with `or`, the right-hand operand may depend on short-circuit evaluation for safety (e.g., `i == 0 or nums[i-1] == 0`). Suggesting `0 in (i, nums[i-1])` would eagerly evaluate both operands, potentially causing IndexError or other exceptions. Skip the FURB108 suggestion when non-common operands contain subscript expressions (IndexExpr) or function calls (CallExpr), as these may raise exceptions or have side effects when eagerly evaluated. Fixes dosisod#350 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
Superseded by #369 which uses a cleaner approach: positive allowlist of safe expression types ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #350.
When comparing values with
or, the right-hand operand may depend on short-circuit evaluation for safety. For example:FURB108 previously suggested
0 in (cutoff, events[cutoff - 1]), which eagerly evaluates both operands and can raiseIndexErrorwhencutoff == 0.This PR skips the FURB108 suggestion when non-common operands contain subscript expressions (
IndexExpr) or function calls (CallExpr), as these may raise exceptions or have side effects when eagerly evaluated. Simple comparisons likex == "abc" or x == "def"continue to be flagged normally.This aligns with the "only allow basic comparisons" approach from the maintainer's comment on #350, as a conservative first step toward better safe/unsafe detection.
Test plan
test/data/err_108.pyfor subscript and call operandscutoff == 0 or events[cutoff - 1] == 0) no longer triggers