AutosarC++18_10-A7.6.1ΒΆ

Functions declared with the [[noreturn]] attribute shall not return

Required inputs: IR

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 [10]: 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