CodeQL documentation

Declaration hides variable

ID: cpp/declaration-hides-variable
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
   - 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 that hide a local variable from a surrounding scope. 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.


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


void f() {
	int i = 10;

	for (int i = 0; i < 10; i++) { //the loop counter hides the variable

		int i = 12; //this variable hides the variable in the outer block


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

  • © GitHub, Inc.
  • Terms
  • Privacy