feat: Add variable shadowing detection in nested scopes#477
Merged
Gbangbolaoluwagbemiga merged 2 commits intoHyperSafeD:mainfrom Mar 30, 2026
Conversation
- Implement VariableShadowingRule to detect variables in nested scopes that shadow outer variables - Track variable declarations across function, block, closure, for-loop, and match arm scopes - Skip common patterns like underscore-prefixed variables and 'env' parameter - Add comprehensive test suite with 10 test cases covering various shadowing scenarios - Integrate rule into CLI analyze command with formatted output - Add example contract demonstrating shadowing bugs and correct alternatives - All tests pass (146 tests in sanctifier-core) This helps prevent logic bugs where the wrong variable is updated or accessed due to shadowing in nested scopes, which is a common source of errors in smart contracts.
|
@meshackyaro Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Description
This PR implements a new static analysis rule to detect variable shadowing in nested scopes, which can lead to logic bugs where the wrong variable is updated or accessed.
Closes #437
Changes
Core Implementation
VariableShadowingRuleintooling/sanctifier-core/src/rules/variable_shadowing.rs_prefixed variables,env,e,self)Testing
CLI Integration
sanctifier analyzecommandExample Contract
contracts/shadowing-exampledemonstrating:Example Output
Why This Matters
Variable shadowing can cause subtle logic bugs in smart contracts where developers think they're updating one variable but are actually modifying a shadowed version. This is especially dangerous in financial applications where such bugs can lead to losses.
Testing
All tests pass successfully.