C++ Logo

std-proposals

Advanced search

alx-0088r0 - noreturn can't return

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Sat, 14 Feb 2026 22:51:32 +0100
Hi!

Here's another proposal for constraining attributes. This is likely to
be even less controversial than that of constraining [[reproducible]]
and [[unsequenced]]. It's worded for C2y, but the concept applies to
C++ too.


Have a lovely night!
Alex

---
Name
 alx-0088r0 - noreturn can't return
Principles
 -  Codify existing practice to address evident deficiencies
 -  Enable secure programming
 And from the old C23 charter:
 -  Trust the programmer, as a goal, is outdated in respect to
    the security and safety programming communities.
Category
 Earthly demon.
Author
 Alejandro Colomar <alx_at_[hidden]>
History
 <https://www.alejandro-colomar.es/src/alx/alx/std/wg14/alx-0088.git/>
 r0 (2026-02-14):
 -  Initial draft.
Description
 It makes absolutely no sense to have a [[noreturn]] function
 that contains a return statement.
  [[noreturn]]
  void f(void)
  {
   return;
   // would you mind serving me a dose of Nasal Demons?
  }
 More work would be needed to make sure that [[noreturn]]
 functions can't return.  This is a good start, though.  A low-
 hanging fruit.
Prior art
 Both GCC and Clang diagnose --by default--, even in the case
 where the return statement is unreachable.
  alx_at_devuan:~/tmp$ cat nr.c
  [[noreturn]]
  void f(void)
  {
   if (0)
    return;
   for (;;)
    continue;
  }
  alx_at_devuan:~/tmp$ gcc -S -O3 nr.c 
  nr.c: In function ‘f’:
  nr.c:5:17: warning: function declared ‘noreturn’ has a ‘return’ statement
      5 |                 return;
        |                 ^~~~~~
  alx_at_devuan:~/tmp$ clang -S -O3 nr.c 
  nr.c:5:3: warning: function 'f' declared 'noreturn' should
        not return [-Winvalid-noreturn]
      5 |                 return;
        |                 ^
  1 warning generated.
Proposed wording
 Based on N3783.
    6.7.5  Function specifiers
 @@ Constraints, p5+1
 +A function declared with a <b>_Noreturn</b> function specifier
 +shall not contain a return statement.
    6.7.12.7  The noreturn and _Noreturn attributes
 @@ Constraints, p2+1
 +A function declared with a <b>noreturn</b> attribute
 +shall not contain a return statement.
-- 
<https://www.alejandro-colomar.es>

Received on 2026-02-14 21:51:45