AutosarC++17_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 271715 2017-03-23 10:13:51Z piotr.tanski $
#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 [7]: AV Rule 189 The goto statement shall not be used.
  • C++ Core Guidelines [10]: ES.76: Avoid goto.
  • C++ Core Guidelines [10]: 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