Skip to content

Fixing Variable Shadowing in Solidity: uint256 Redeclaration Error #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2024
Merged

Conversation

rnkrtt
Copy link
Contributor

@rnkrtt rnkrtt commented Dec 4, 2024

The issue arises because the variable a is already declared as a parameter of the arithmeticError function:

function arithmeticError(uint256 a) public {
    uint256 a = a - 100; // Error: `a` is already declared as a parameter
}

When you redeclare a with uint256 a = a - 100;, the compiler throws an error because you're trying to shadow the parameter a by declaring a new variable with the same name. This is not allowed in Solidity.

To fix this, simply remove the type declaration (uint256) and use the parameter a directly:

solidity
Code kopieren

function arithmeticError(uint256 a) public {
    a = a - 100; // Correct: modifies the existing parameter `a`
}

Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@rnkrtt
Copy link
Contributor Author

rnkrtt commented Dec 6, 2024

@DaniPopes Hello! Can someone take a look at this pull request and merge it if everything looks good? Let me know if any adjustments are needed. Appreciate it!

@rnkrtt rnkrtt requested a review from DaniPopes December 26, 2024 15:22
@rnkrtt
Copy link
Contributor Author

rnkrtt commented Dec 26, 2024

@DaniPopes

@DaniPopes DaniPopes merged commit b5a8691 into foundry-rs:master Dec 27, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants