blob: fcb7dd313259c91fe08a1f3b6739e76db64eabd9 [file] [log] [blame]
.. title:: clang-tidy - cppcoreguidelines-avoid-goto
The usage of ``goto`` for control flow is error prone and should be replaced
with looping constructs. Only forward jumps in nested loops are accepted.
This check implements `ES.76 <>`_
from the CppCoreGuidelines and
`6.3.1 from High Integrity C++ <>`_.
For more information on why to avoid programming
with ``goto`` you can read the famous paper `A Case against the GO TO Statement. <>`_.
The check diagnoses ``goto`` for backward jumps in every language mode. These
should be replaced with `C/C++` looping constructs.
.. code-block:: c++
// Bad, handwritten for loop.
int i = 0;
// Jump label for the loop
if (i < 100) {
goto loop_start;
// Better
for(int i = 0; i < 100; ++i)
Modern C++ needs ``goto`` only to jump out of nested loops.
.. code-block:: c++
for(int i = 0; i < 100; ++i) {
for(int j = 0; j < 100; ++j) {
if (i * j > 500)
goto early_exit;
All other uses of ``goto`` are diagnosed in `C++`.