C++ Logo

sg12

Advanced search

[SG12] Missing non-void return on some paths

From: JF Bastien <cxx_at_[hidden]>
Date: Thu, 6 May 2021 16:47:14 -0700
Hello 🐉 UB 🐲,
(resending.. again... with the right address.... darned email UB)

A recent MISRA discussion makes me wonder: *why do we keep this UB around?*

Shafik's paper has a short mention here: http://wg21.link/P1705#stmtreturn
 http://wg21.link/p2234 <http://wg21.link/p2234> also talks about this.

The specific wording <http://eel.is/c++draft/stmt.return#2.sentence-8>:

Flowing off the end of a constructor, a destructor, or a non-coroutine
function with a cv void return type is equivalent to a return with no
operand. Otherwise, flowing off the end of a function other than main or a
coroutine results in undefined behavior.


We have [[noreturn]] to help express programmer intent around this, and
we've got a proposal for std::unreachable <http://wg21.link/p0627> (waiting
for an update post LWG feedback
<https://github.com/cplusplus/papers/issues/275>) which IMO allows
expressing intent which [[noreturn]] doesn't express.

Compilers diagnose when functions can't be proved to return, and I wouldn't
work on a codebase without this diagnostic enabled as an error. Is there a
valid reason to keep this UB around? I get that exceptions and longjmp and
exit make this diagnostic conservative, but I'd rather have programmers
express intent with [[noreturn]] and std::unreachable, which as far as I
know compilers diagnose 100% accurately when used to express intent. It
seems like we're keeping UB around when we have better tools ([[noreturn]]
 and std::unreachable) to opt-in to UB.

Thanks!

JF

Received on 2021-05-06 18:47:28