Use of goto¶
ID: cpp/use-of-goto
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- maintainability
- readability
- language-features
Query suites:
- cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
Use of goto
statements makes code more difficult to understand and maintain. Consequently, the use of goto
statements is deprecated except as a mechanism for breaking out of multiple nested loops, or jumping to error-handling code at the end of a function. This rule identifies complex (and therefore hard to understand) uses of goto
statements. Function bodies that include multiple goto
statements that jump forward and multiple goto
statements that jump backwards are highlighted.
Recommendation¶
In most cases the code can be rewritten and/or rearranged by:
using structured control flow constructs, such as
if
,while
, andfor
;using
break
orcontinue
to stop a loop iteration early; orusing
return
to exit a function early Thegoto
statement may be the best solution for breaking out of deeply nested loops, or for jumping to error handling code, without adversely affecting the readability of the function. Such uses will not be flagged by this rule.
References¶
Joint Strike Fighter Air Vehicle C++ Coding Standards, AV Rule 189. Lockheed Martin Corporation, 2005.
E. W. Dijkstra Archive: A Case against the GO TO Statement (EWD-215).
MSDN Library: goto Statement (C++).
Mats Henricson and Erik Nyquist, Industrial Strength C++, Rule 4.6. Prentice Hall PTR, 1997. (PDF).
cplusplus.com: Control Structures.