CodeQL documentation

Unused static variable

ID: cpp/unused-static-variable
Kind: problem
Severity: recommendation
Precision: high
Tags:
   - efficiency
   - useless-code
   - external/cwe/cwe-563
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds static variables that are never accessed. These static variables should be removed, to increase code comprehensibility, reduce memory usage and avoid misuse. Unused static const variables could also be an indication of a defect caused by an unhandled case.

Recommendation

Check that the unused static variable does not indicate a defect, for example, an unhandled case. If the static variable is genuinuely not needed, then removing it will make code more readable. If the static variable is needed then you should update the code to fix the defect.

Example

void f() {
	static int i = 0; //i is unused
	...
	return;
}