Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong clang-tidy warning about static global lambda variables?

Provided the following code, in the global scope, clang-tidy gives no warning:

auto test = []{};

However, when doing the following, it does:

#include <tuple>

auto test = []{
    std::tuple t{1, 2, 3};
};
<source>:3:6: warning: initialization of 'test' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
auto test = []{
     ^

/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:646:19: note: possibly throwing constructor declared here
        constexpr tuple(_UElements&&... __elements)
                  ^

Marking the lambda as noexcept doesn't help.

However, I don't see why that would be a problem. The exception could only theorically happen when invoking the lambda, wouldn't it?

The following code does not cause the warning to appear:

auto test = [] {
    throw 0;
};

Is clang-tidy wrong, or did I miss something?

like image 416
Asu Avatar asked Nov 06 '22 23:11

Asu


1 Answers

The Clang-Tidy warning is about the construction of the global variable, not about the operator() of such class does. Hence, this looks like a false positive.

I would make the variable constexpr as it can't change during the lifetime of your program. That should as well suppress the warning as well as constexpr can't be done of such an exception is thrown.

PS: You can log bugs for Clang-Tidy at bugs.llvm.org

like image 180
JVApen Avatar answered Nov 15 '22 12:11

JVApen