Variable used in its own initializer¶
ID: cpp/use-in-own-initializerKind: problemSecurity severity: Severity: warningPrecision: highTags: - maintainability - correctnessQuery 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 asizeof expression.
Example¶
intf(){intx=x;// BAD: undefined behavior occurs herex=0;returnx;}intg(){intx=0;// GOODreturnx;}