CodeQL documentation

Variable used in its own initializer

ID: cpp/use-in-own-initializer
Kind: problem
Severity: warning
Precision: high
Tags:
   - maintainability
   - correctness
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

A variable is in scope in its own initializer, but it is undefined behavior to load from it before it is first assigned to.

Recommendation

Do not use a variable in its own initializer unless it is part of an address calculation or a sizeof expression.

Example

int f() {
	int x = x; // BAD: undefined behavior occurs here
	x = 0;
	return x;
}

int g() {
	int x = 0; // GOOD
	return x;
}