The code checks std::current_exception() just before the rethrow, so there is always a current exception here. Without the if guard, the same function produces no warning.
A dispatcher written as a lambda gets the same error:
It looks like the exemption in CheckExceptionSafetyImpl::rethrowNoCurrentException() (lib/checkexceptionsafety.cpp:384) only matches try { throw ; } catch ( at functionScope->bodyStart->next() of a named function. Any statement before the try disables the exemption, and lambdas have no Function, so they are scanned as part of the enclosing function.
Reproduced with 2.20.1 and current main (530e59f).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
Cppcheck reports an error for the "exception dispatcher" idiom when the
try { throw; }is not the first statement of the function:The code checks
std::current_exception()just before the rethrow, so there is always a current exception here. Without theifguard, the same function produces no warning.A dispatcher written as a lambda gets the same error:
It looks like the exemption in
CheckExceptionSafetyImpl::rethrowNoCurrentException()(lib/checkexceptionsafety.cpp:384) only matchestry { throw ; } catch (atfunctionScope->bodyStart->next()of a named function. Any statement before thetrydisables the exemption, and lambdas have noFunction, so they are scanned as part of the enclosing function.Reproduced with 2.20.1 and current main (530e59f).