【问题标题】:Wrong clang-tidy warning about static global lambda variables?关于静态全局 lambda 变量的错误警告?
【发布时间】:2019-04-27 07:38:23
【问题描述】:

提供以下代码,在全局范围内,clang-tidy 不会给出警告:

auto test = []{};

但是,当执行以下操作时,它会:

#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)
                  ^

将 lambda 标记为 noexcept 没有帮助。

但是,我不明白为什么会有问题。理论上,只有在调用 lambda 时才会发生异常,不是吗?

以下代码不会导致出现警告:

auto test = [] {
    throw 0;
};

clang-tidy 是不是错了,还是我错过了什么?

【问题讨论】:

    标签: c++ lambda clang-tidy


    【解决方案1】:

    Clang-Tidy 警告是关于全局变量的构造,而不是关于此类的operator()。因此,这看起来像是误报。

    我会创建变量constexpr,因为它在程序的生命周期内无法更改。这也应该抑制警告以及 constexpr 不能完成这样的异常被抛出。

    PS:您可以在 bugs.llvm.org 记录 Clang-Tidy 的错误

    【讨论】:

    • 实际上...使用constexpr仍然实际上会发出警告。到时候我会报告给 LLVM,谢谢!
    • 听起来像第二个错误:)
    猜你喜欢
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2022-01-28
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多