CodeQL documentation

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, and for;

  • using break or continue to stop a loop iteration early; or

  • using return to exit a function early The goto 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

  • © GitHub, Inc.
  • Terms
  • Privacy