首页 > 解决方案 > clang-tidy 10 忽略了我的 NOLINT 命令

问题描述

clang-tidyv10.0.0 似乎忽略了我的NOLINTorNOLINTNEXTLINE指示。使用这个微不足道的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指令?

标签: c++clang-tidy

解决方案


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

您可以clang-tidy通过传入命令行定义来忽略这些部分,例如-DCLANG_TIDY, 然后在您想要忽略的代码中使用#ifndef CLANG_TIDY...块。#endifclang-tidy

这是一个务实的解决方法。


推荐阅读