'Clang-Tidy `NOLINT` for multiple lines?

I'm working on a C++ project that has some large sections of code that are autogenerated, and I don't want to be linted. Is there something akin to the //NOLINT comment that can be applied to multiple lines? Like the following:

// BEGINNOLINT
bad-code;
// ENDNOLINT

All I could find online was a suggestion that this should be implemented. Is there any way to avoid having to write // NOLINT on the end of every single line?



Solution 1:[1]

clang-tidy 14 introduced this feature:

// NOLINTBEGIN
...
// NOLINTEND

Note, if you want to disable a specific warning, the end-comment must match the begin-comment:

// NOLINTBEGIN(check-name)
...
// NOLINTEND(check-name)

Solution 2:[2]

Unfortunately there is no direct way to do this, clang-tidy only supports //NOLINT and //NOLINTNEXTLINE.

I don't know how much control you have about that code generation...you could generate it in one line, that could help you a lot as c++ doesn't care about whitespace.

A crude but effective solution is to use a text-manipulation tool like sed:

$ sed -i -re '/^\/\/BEGIN_NOLINT/,/^\/\/END_NOLINT/{s/$/\/\/NOLINT/}' *.cpp

This would add //NOLINT at the end of every line between //BEGIN_NOLINT and //END_NOLINT comments (which can be probably generated).

You can also run clang-tidy with parameter

-line-filter='[{"name":"test.cpp","lines":[[1,10],[12,100]]}]'

Line 11 will be skipped in this example. This is however difficult to maintain as you need to update the filter every time you add/remove lines in the file. Maybe it would be a good idea to generate code into separate files if possible.

Solution 3:[3]

Im sure that, when Im applying //NOLINTNEXTLINE on MACRO, so the entire MARCO is skiped. So try to implement //NOLINTNEXTLINE to your class or function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Alexandr Zinchenko