fix: do not flag block argument shadowing across mutually exclusive branches in Lint/ShadowingOuterLocalVar#824
Merged
Sija merged 2 commits intoMay 11, 2026
Conversation
…ranches in `Lint/ShadowingOuterLocalVar`
The rule used a purely positional check (`declared_before?`) to decide
whether a block or proc argument shadowed an outer local variable. As a
result it produced false positives whenever the outer assignment lived
in a control-flow branch that could not reach the block argument:
```cr
if cond
x = 1
else
foo { |x| } # incorrectly flagged
end
```
Add a localized AST-walking check that records the branch path of the
block argument and each assignment of the candidate variable. When every
assignment diverges from the argument's path at a common branching
ancestor, the outer assignment cannot reach the block argument and the
warning is suppressed.
Handles `if`/`unless`, `case`/`when`/`else`, `select`/`when`/`else`, and
`begin`/`rescue`/`else` (body and `ensure` always run, so they remain
non-exclusive). The visitor is cached per outer scope across candidates
within a single rule invocation to avoid re-walking the same AST.
Sija
reviewed
May 11, 2026
…lly exclusive branches
Lint/ShadowingOuterLocalVar
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.
Closes #819
Added a localized AST-walking check (
mutually_exclusive_branches?) that records the branch path of the block argument and each assignment of the candidate variable. When every assignment diverges from the argument's path at a common branching ancestor, the outer assignment cannot reach the block argument and the warning is suppressed.No
Branch/Branchableinfrastructure was reintroduced (recently removed in #795) — the check is contained in the rule.