CodeQL documentation

Local variable hides global variable

ID: cpp/local-variable-hides-global-variable
Kind: problem
Security severity: 
Severity: recommendation
Precision: very-high
Tags:
   - maintainability
   - readability
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds declarations of local variables or parameters that hide a global variable. Such declarations create variables with the same name but different scopes. This makes it difficult to know which variable is actually used in an expression.

Recommendation

Consider changing the name of either variable to keep them distinct.

Example

int i = 10;

void f() {
	for (int i = 0; i < 10; i++) { //the loop counter hides the global variable i
		 ...
	}

	{
		int i = 12; //this variable hides the global variable i
		...
	}
}

References

  • B. Stroustrup. The C++ Programming Language Special Edition p 82. Addison Wesley. 2000.

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy