Can Clang warn me when I might throw an exception from a `noexcept` destructor?

740 views Asked by At

C++11 specifies destructors as noexcept by default. Is there a way I can get Clang to report cases where my noexcept destructors might throw an exception (and hence call std::terminate)?

1

There are 1 answers

3
Deduplicator On BEST ANSWER

First, C++ does not specify destructors as noexcept by default.
It specifies them as noexcept(all subobjects destructors are noexcept).

Next, we can categorize expressions and statements in one category each of:

  1. Never returns, may return, always returns.
  2. Never throws, may throw, always throws.

In all of them, only the first case will be marked as exceptional.

In order to give good results, the compiler must be able to analyze the program behavior sufficiently to determine whether for any possible state, a throwing expression is ever executed.

If you are happy with far too many warnings about impossible scenarios, you can get a result here.
Conversely, if you only want to warn about blatant cases, you might also get some results.

Trouble is, all the interesting cases devolve to solving the halting problem.
And no, we are no further along with it.