AutosarC++18_03-A6.6.1¶
The goto statement shall not be used
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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 [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
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.