62 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-avoid-goto2 3cppcoreguidelines-avoid-goto4============================5 6The usage of ``goto`` for control flow is error prone and should be replaced7with looping constructs. Only forward jumps in nested loops are accepted.8 9This check implements `ES.7610<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es76-avoid-goto>`_11from the C++ Core Guidelines and12`6.3.1 <https://www.perforce.com/resources/qac/high-integrity-cpp-coding-standard/statements>`_13from High Integrity C++ Coding Standard.14 15For more information on why to avoid programming16with ``goto`` you can read the famous paper `A Case against the GO TO Statement. <https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF>`_.17 18The check diagnoses ``goto`` for backward jumps in every language mode. These19should be replaced with `C/C++` looping constructs.20 21.. code-block:: c++22 23 // Bad, handwritten for loop.24 int i = 0;25 // Jump label for the loop26 loop_start:27 do_some_operation();28 29 if (i < 100) {30 ++i;31 goto loop_start;32 }33 34 // Better35 for(int i = 0; i < 100; ++i)36 do_some_operation();37 38Modern C++ needs ``goto`` only to jump out of nested loops.39 40.. code-block:: c++41 42 for(int i = 0; i < 100; ++i) {43 for(int j = 0; j < 100; ++j) {44 if (i * j > 500)45 goto early_exit;46 }47 }48 49 early_exit:50 some_operation();51 52All other uses of ``goto`` are diagnosed in `C++`.53 54 55Options56-------57 58.. option:: IgnoreMacros59 60 If set to `true`, the check will not warn if a ``goto`` statement is61 expanded from a macro. Default is `false`.62