clang整洁10忽略了我的NOLINT命令

clang-tidy 10 is ignoring my NOLINT commands

本文关键字:我的 NOLINT 命令 整洁 clang      更新时间:2023-10-16

clang-tidyv10.0.0似乎忽略了我的NOLINTNOLINTNEXTLINE指令。使用这个琐碎的compile_commands.json:

[
{
"directory": "/home/cmannett85/workspace/scratch/build",
"command": "/usr/lib/ccache/g++-10 -g -Werror -Wall -Wextra -std=c++2a -o main.cpp.o -c /home/cmannett85/workspace/scratch/main.cpp",
"file": "/home/cmannett85/workspace/scratch/main.cpp"
}
]

这个琐碎的源文件:

#include <ranges>
#include <vector>
#include <iostream>
int main()
{
auto v = std::vector{0, 1, 2, 3, 4};
for (auto i : v | std::views::reverse) { // NOLINT
std::cout << i << std::endl;
}
return EXIT_SUCCESS;
}

产生此clang-tidy输出:

$ clang-tidy -p . --quiet ./main.cpp 
2 warnings and 6 errors generated.
Error while processing /home/cmannett85/workspace/scratch/main.cpp.
/home/cmannett85/workspace/scratch/main.cpp:8:21: error: invalid operands to binary expression ('std::vector<int, std::allocator<int> >' and 'const __adaptor::_RangeAdaptorClosure<(lambda at /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ranges:3280:9)>' (aka 'const std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::(lambda at /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ranges:3280:9)>')) [clang-diagnostic-error]
for (auto i : v | std::views::reverse) { // NOLINT
...

现在我可以原谅这个虚假的错误,因为C++20范围支持可能缺乏,因为它是如此的新,但为什么clang-tidy忽略了我的NOLINT指令?

clang-tidy程序有时会出现误报,或者以其他方式识别出有问题的区域,这些区域可能是(给定您的平台(已知的实现定义的行为,可能不一定符合标准。

您可以通过传入命令行定义(如-DCLANG_TIDY(让clang-tidy忽略这些部分,然后使用#ifndef CLANG_TIDY。。。您希望clang-tidy忽略的代码中的#endif块。

这是一个务实的变通办法。