Fix false positive in UnusedVariables for dict key subscripts#248
Open
frankentini wants to merge 1 commit intotrailofbits:masterfrom
Open
Fix false positive in UnusedVariables for dict key subscripts#248frankentini wants to merge 1 commit intotrailofbits:masterfrom
frankentini wants to merge 1 commit intotrailofbits:masterfrom
Conversation
…fbits#226) Variables used as dictionary keys in subscript assignments (e.g. _var_dict[_var16] = value, generated by SETITEM opcodes) were not being recognized as 'used', causing them to be incorrectly flagged as suspicious unused variables by --check-safety. The root cause: unused_assignments() only walked ast.Assign.value for Name references, but subscript targets like _var_dict[_var16] contain Name nodes in the target's slice that were never visited. Fix: when an assignment target is not a plain ast.Name (e.g. it's an ast.Subscript), walk the entire target subtree and add any Name nodes found to the 'used' set. Adds regression test using a pickle with SETITEM dict-key pattern.
|
|
Collaborator
|
@frankentini Can you please sign the CLA before we review both of your PRs? Thanks! |
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 #226.
Variables used as dictionary keys in subscript assignments (e.g.
_var_dict[_var16] = value, generated bySETITEMopcodes) were not being recognized as "used" byunused_assignments(), causing--check-safetyto incorrectly flag them as suspicious.Root Cause
unused_assignments()iterates over statements and, forast.Assignnodes, only records top-levelast.Nametargets as definitions, then walksstatement.valuefor used names. However, when SETITEM creates an assignment like:The target is an
ast.Subscript, not anast.Name. The variable_var16used as the subscript key lives inside the target and was never walked for uses -- so it appeared indefined - useddespite being genuinely referenced.Fix
When an assignment target is not a plain
ast.Name(e.g.ast.Subscript,ast.Starred), walk the entire target subtree and add anyast.Namenodes found to theusedset.Test
Added regression test
test_unused_variables_no_false_positive_for_dict_keysthat constructs a pickle with the SETITEM dict-key pattern and verifies no false positives are reported.