Bug 94112 - Please add a warning for potentially throwing code in noexcept function
Summary: Please add a warning for potentially throwing code in noexcept function
Status: RESOLVED DUPLICATE of bug 61372
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2020-03-10 01:09 UTC by Rafael Avila de Espindola
Modified: 2021-10-27 05:17 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-03-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rafael Avila de Espindola 2020-03-10 01:09:47 UTC
GCC produces a warning for

void foo() noexcept {
    throw 42;
}

But not for

static void bar() {
    throw 42;
}
void foo() noexcept {
    bar();
}

Such a warning would be quite handy for checking the usage of noexcept in a codebase.
Comment 1 Martin Sebor 2020-03-10 15:34:53 UTC
Confirmed with the output below.  -Wterminate is fully implemented in the C++ front-end so it doesn't know about what might happen in called functions.  Implementing the detection across inlined functions requires moving the warning to the middle-end and deciding how to deal with situations where the called function throws only under some non-constant conditions (e.g., issue -Wterminate regardless, or a -Wmaybe-terminate kind of a warning, or none).

$ gcc -O2 -Wall -Wextra -S -Wall a.C
void foo () noexcept
{
    throw 42;
}

static void bar ()
{
    throw 42;
}

void foobar () noexcept
{ 
    bar ();
}

a.C: In function ‘void foo()’:
a.C:3:5: warning: ‘throw’ will always call ‘terminate’ [-Wterminate]
    3 |     throw 42;
      |     ^~~~~~~~
Comment 2 Rafael Avila de Espindola 2020-03-10 15:53:02 UTC
(In reply to Martin Sebor from comment #1)
> Confirmed with the output below.  -Wterminate is fully implemented in the
> C++ front-end so it doesn't know about what might happen in called
> functions.  Implementing the detection across inlined functions requires
> moving the warning to the middle-end and deciding how to deal with
> situations where the called function throws only under some non-constant
> conditions (e.g., issue -Wterminate regardless, or a -Wmaybe-terminate kind
> of a warning, or none).


I would be happy even with a "noexcept function calls a non-noexcept one" warning.
Comment 3 Jonathan Wakely 2020-03-10 16:01:30 UTC
That would likely be far too noisy for use 99% of the time.
Comment 4 Eric Gallager 2021-10-27 05:17:26 UTC
dup of bug 61372

*** This bug has been marked as a duplicate of bug 61372 ***