Home > Software design >  How can I silence all errors of a particular kind in clang-tidy?
How can I silence all errors of a particular kind in clang-tidy?

Time:07-09

I'm currently working on a university assignment where it is mandated that we use C-style arrays for our code. Infuriatingly, they have left the default setting for warning about C-style arrays, which means that every time I use a one I need to add a // NOLINTNEXTLINT(modernize-avoid-c-arrays), which is absolutely terrible in terms of readability.

I have tried silencing the errors using // NOLINTBEGIN(modernize-avoid-c-arrays), but since that was only introduced in clang-tidy-14, it isn't supported by our course-mandated clang-tidy-11.

As such, my only option for a clean way of silencing these errors is by modifying the configuration file. Unfortunately, I haven't found any information on how to silence errors of a particular type in the configuration file. In fact, I haven't found any documentation on the expected structure of the .clang-tidy file at all, except for some example files, none of which appear to silence any errors.

How can I silence the modernize-avoid-c-arrays error on a project-wide basis from my .clang-tidy file?

CodePudding user response:

Disable a check

To disable a check in .clang-tidy, add a Checks: line, and as its value put the name of the check, preceded by a hyphen (meaning to disable rather than enable). For example, here is a complete .clang-tidy file that first enables all of the modernize-* checks (since they are not enabled by default), then disables modernize-avoid-c-arrays:

Checks: 'modernize-*,-modernize-avoid-c-arrays'

Documentation of .clang-tidy format

The .clang-tidy format is (tersely!) specified in the output of clang-tidy --help:

  --config-file=<string>         -
                                  Specify the path of .clang-tidy or custom config file:
                                    e.g. --config-file=/some/path/myTidyConfigFile
                                  This option internally works exactly the same way as
                                    --config option after reading specified config file.
                                  Use either --config-file or --config, not both.

However, the --config-file option and its documentation are missing in Clang LLVM-11, so that may be why you didn't find it. They are in Clang LLVM-14 (I'm not sure which version introduced that option). Nevertheless, Clang LLVM-11 responds to the presence of .clang-tidy.

You can use the --dump-config option to have clang-tidy print its current configuration in the .clang-tidy syntax, then trim or edit as desired.

Related:

Complete example

Input source code:

// test.cpp
// Test input for clang-tidy.

void f()
{
  int arr[3];
  bool b = 1;
}

// EOF

Run using the above .clang-tidy:

$ /home/scott/opt/clang llvm-11.0.1-x86_64-linux-gnu-ubuntu-16.04/bin/clang-tidy test.cpp --
1 warning generated.
/home/scott/wrk/learn/clang/clang-tidy-config/test.cpp:7:12: warning: converting integer literal to bool, use bool literal instead [modernize-use-bool-literals]
  bool b = 1;
           ^
           true

Without disabling modernize-avoid-c-arrays, two warnings would be printed. (And without enabling modernize-*, nothing is printed.)

  • Related