Variable hides another variable of same name with nested scope
This defect occurs when a variable hides another variable of the same name in an outer scope.
For instance, if a local variable has the same name as a global variable, the local variable hides the global variable during its lifetime.
When two variables with the same name exist in an inner and outer scope, any reference to the variable name uses the variable in the inner scope. However, a developer or reviewer might incorrectly expect that the variable in the outer scope was used.
The fix depends on the root cause of the defect. For instance, suppose you refactor a function such that you use a local static variable in place of a global variable. In this case, the global variable is redundant and you can remove its declaration. Alternatively, if you are not sure if the global variable is used elsewhere, you can modify the name of the local static variable and all references within the function.
If the shadowing is intended and you do not want to fix the issue, add comments to your result or code to avoid another review. See Address Results in Polyspace Access Through Bug Fixes or Justifications.
| Group: Data flow |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax: VAR_SHADOWING |
| Impact: Low |