RED-local-hides-global
In this section:
Synopsis
The definition of a local variable hides a global definition.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
A local variable is declared with the same name as a global variable, hiding the global variable from this scope, from this point onwards. This might be intentional, but it is better to use a different name for the local variable, so that a reference to the global variable does not accidentally change or return the local value.
Coding standards
- CERT DCL01-C
Do not reuse variable names in subscopes
- CERT DCL01-CPP
Do not reuse variable names in subscopes
Code examples
The following code example fails the check and will give a warning:
int x;
int foo (int y ) {
int x=0;
x++;
return x+y;
}
The following code example passes the check and will not give a warning about this issue:
int x;
int foo (int y ) {
x++;
return x+y;
}