4

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?

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • 4
    `NOLINT` suppresses clang-tidy warnings. It does not suppress errors. – Eljay Jul 10 '20 at 16:37
  • @Eljay Ugh. Now I've reread the documentation, it does specifically only say warnings. Is there are way of doing the equivalent for errors? – cmannett85 Jul 10 '20 at 17:08
  • `clang-tidy -DCLANG_TIDY -p . --quiet ./main.cpp` and then `#ifndef CLANG_TIDY` ... `#endif` in the code. – Eljay Jul 10 '20 at 17:12
  • @Eljay thanks, it's clunky but understandable. Make your comment an answer and I'll accept it. – cmannett85 Jul 10 '20 at 17:49

1 Answers1

3

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.

Eljay
  • 4,648
  • 3
  • 16
  • 27