AutosarC++18_03-A7.6.1ΒΆ
Functions declared with the [[noreturn]] attribute shall not return
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
The C++ standard specifies that functions with the [[noreturn]] attribute shall not return.
Returning from such a function can be prohibited in the following way: throwing an exception, entering an infinite loop, or calling another function with the [[noreturn]] attribute. Returning from such a function leads to undefined behavior.
1 // $Id: A7-6-1.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $ 2 #include <cstdint> 3 #include <exception> 4 5 class PositiveInputException : public std::exception {}; 6 7 [[noreturn]] void f(int i) //non-compliant 8 { 9 if (i > 0) 10 { 11 throw PositiveInputException(); 12 } 13 //undefined behaviour for non-positive i 14 } 15 16 [[noreturn]] void g(int i) //compliant 17 { 18 if (i > 0) 19 { 20 throw "Received positive input"; 21 } 22 23 while(1) 24 { 25 //do processing 26 } 27 }
See Also
- SEI CERT C++ Coding Standard [9]: MSC53-CPP: Do not return from a function declared [[noreturn]].
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
noreturn_violation |
Do not return from a noreturn function. |
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.