Search code examples
c++clang-tidy

clang-tidy 10 is ignoring my NOLINT commands


clang-tidy v10.0.0 appears to ignore my NOLINT or NOLINTNEXTLINE instructions. Using this trivial 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"
}
]

And this trivial source file:

#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;
}

Yields this clang-tidy output:

$ 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
        ...

Now I can forgive the spurious error as C++20 ranges support may be lacking as it is so new, but why is clang-tidy ignoring my NOLINT instruction?


Solution

  • The clang-tidy program can sometimes turn up false positives, or otherwise identify areas problematic that maybe (given your platform) are known implementation defined behavior that may not necessarily be compliant with the standard.

    You can have clang-tidy ignore those sections by passing in a command-line define, such as -DCLANG_TIDY, and then use #ifndef CLANG_TIDY ... #endif blocks in your code that you want clang-tidy to ignore.

    It is a pragmatic workaround.