Rule A7-6-1 (required, implementation, automated)
Functions declared with the [[noreturn]] attribute shall not return.
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.
// $Id: A7-6-1.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <cstdint>
#include <exception>
class PositiveInputException : public std::exception {};
[[noreturn]] void f(int i) //non-compliant
{
if (i > 0)
{
throw PositiveInputException();
}
//undefined behaviour for non-positive i
}
[[noreturn]] void g(int i) //compliant
{
if (i > 0)
{
throw "Received positive input";
}
while(1)
{
//do processing
}
}
See also
SEI CERT C++ Coding Standard [10]: MSC53-CPP: Do not return from a function declared [[noreturn]].