clang-tidy 未能通过易出错检查

clang-tidy fails bugprone check

我正在使用以下文件试验 clang-tidy

#include <stdio.h>
int main(int argc, char **argv)
{
    int i=2; int j=1;

    if (argc = 5) { return 2; }
    while (i<argc) { j++; }

    return 0;
}

我的目标是 检测无限循环 具有:

$ clang-tidy -checks=bugprone-infinite-loop main.c

但是 clang-tidy 找到的只是 = 而不是 == 东西:

Error while trying to load a compilation database:
Could not auto-detect compilation database for file "main.c"
No compilation database found in /home/oren or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 warning generated.
/home/oren/main.c:6:11: warning: using the result of an assignment as a condition without parentheses [clang-diagnostic-parentheses]
        if (argc = 5) { return 2; }
            ~~~~ ^  ~
            (    == )
/home/oren/main.c:6:11: note: place parentheses around the assignment to silence this warning
/home/oren/main.c:6:11: note: use '==' to turn this assignment into an equality comparison

您正在使用尚未发布的 LLVM (10.0.0) 版本的功能。

在我的系统 (windows) 上,您的文件按预期工作:

>clang-tidy10 -checks=bugprone-infinite-loop infloop.c --
1 warning generated.
\infloop.c:6:5: warning: this loop is infinite; none of its con
dition variables (i, argc) are updated in the loop body [bugprone-infinite-loop]

while (i<argc) { j++; }
^

我对文件所做的唯一修改是删除了不必要的#include。我还在命令中添加了 --(双破折号)以消除缺少的编译数据库错误。我正在使用来自 https://llvm.org/builds/

的预构建二进制文件

我最好的猜测是您使用的是较旧版本的 clang-tidy,但未检测到这一点。作为参考,我的版本是 10.0.0-e20a1e486e1,您可以通过 运行ning 查看您的版本:

>clang-tidy --version

我还会检查您是否真的运行通过以下方式运行检查您应该运行的支票:

$ clang-tidy -checks=bugprone-infinite-loop main.c --list-checks

P.S。您最初收到的警告消息是基于 clang-diagnostic 的,这与 clang-tidy 无关,而是与 clang compilation