19

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?

user3002473
  • 4,835
  • 8
  • 35
  • 61

3 Answers3

29

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)
GPMueller
  • 2,881
  • 2
  • 27
  • 36
  • 3
    Will it work for whole include files? As in `// NOLINTBEGIN \\ #include
    \\ //NOLINTEND` ?
    – alfC Nov 06 '21 at 01:39
  • the checkname in END seems too redundant, is it really necessary? – alfC Jul 08 '22 at 10:12
  • Just to confirm, yes, `NOLINTEND` check names must match that of `NOLINTBEGIN` otherwise, the end is not recognized, and one gets this `error: unmatched 'NOLINTBEGIN' comment without a subsequent 'NOLINTEND' comment [clang-tidy-nolint]` – alfC Jul 18 '23 at 18:51
6

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.

pablo285
  • 2,460
  • 4
  • 14
  • 38
-2

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