AutosarC++19_03-A6.6.1

The goto statement shall not be used

Required inputs: IR

Rationale

Using goto statement significantly complicates the logic, makes the code difficult to read and maintain, and may lead to incorrect resources releases or memory leaks.

Example
// $Id: A6-6-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #include <cstdint>
    namespace
    {
    constexpr std::int32_t loopLimit = 100;
    }
    void F1(std::int32_t n) noexcept
    {
        if (n < 0)
        {
            // goto exit; // Non-compliant - jumping to exit from here crosses ptr
            // pointer initialization, compilation
            // error
        }

        std::int32_t* ptr = new std::int32_t(n);
    // ...
    exit:
        delete ptr;
    }
    void F2() noexcept
    {
        // ...
        goto error; // Non-compliant
    // ...
    error:; // Error handling and cleanup
    }
    void F3() noexcept
    {
        for (std::int32_t i = 0; i < loopLimit; ++i)
        {
            for (std::int32_t j = 0; j < loopLimit; ++j)
            {
                for (std::int32_t k = 0; k < loopLimit; ++k)
                {
                    if ((i == j) && (j == k))
                    {
                        // ...
                        goto loop_break; // Non-compliant
                    }
                }
               }
          }

    loop_break:;    // ...
    }
See Also
  • JSF December 2005 [8]: AV Rule 189 The goto statement shall not be used.
  • C++ Core Guidelines [11]: ES.76: Avoid goto.
  • C++ Core Guidelines [11]: NR.6: Don’t: Place all cleanup actions at the end of a function and goto exit.

Possible Messages

Key

Text

Severity

Disabled

goto

Do not use goto.

None

False

Options