2

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 // NOLINTNEXTLINE(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?

Miguel Guthridge
  • 1,444
  • 10
  • 27
  • 1
    Honestly, I think you should talk to your TA about this. Clearly this is an organizational issue. If they mandate you use clang-tidy-11, but also mandate a conflicting coding style, then *that's* the issue to be addressed. – datenwolf Jul 08 '22 at 07:01
  • 2
    If you use fixed size arrays you could also ask if it is okay to use std::array, you can do all the same things as with a "C" style array. Or is it a datastructures course where you also have to do things like double pointers, new and delete? Anyway I'm biased since I think courses that use C++ should also teach proper C++ techniques. If I could I would have a chat with your teacher about it) – Pepijn Kramer Jul 08 '22 at 07:02
  • @datenwolf Well said :) – Pepijn Kramer Jul 08 '22 at 07:02
  • 1
    @datenwolf I wish they would care about what students think. Their setup script entirely broke my VS Code config across all of my machines, and installed a ton of programs, some of which broke my updating system, and they were just like "sorry about that I guess" – Miguel Guthridge Jul 08 '22 at 07:04
  • @PepijnKramer agreed that would be a good idea, but they've explicitly said we can't sadly. – Miguel Guthridge Jul 08 '22 at 07:04
  • 1
    The problem that I have with that is that most students will first get to see the "C" style approach and never learn the techniques that will be less buggy. To bad I can't help you with clang-tidy (only use it very sparingly). As for breaking your own environment consider running theirs in a virtual machine. (Ideally they should proved a docker image or a VM on a network). – Pepijn Kramer Jul 08 '22 at 07:08
  • @PepijnKramer technically they do give a VM on a network that we access through VNC, but due to latency I find it an absolute pain to work with, except for doing the final testing before I submit (since it's 32-bit and never seems to compile on the first go, even if it works on my machine). If they gave a docker image, I'd definitely be using that instead, but sadly the one they provided doesn't seem to work for me. – Miguel Guthridge Jul 08 '22 at 07:13
  • 2
    Well good luck on your C++ journey, I think you at least have the right mindset :) Can you ask your teacher if he is aware of this? https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines – Pepijn Kramer Jul 08 '22 at 07:16
  • @MiguelGuthridge: Other than using a VM, my recommendation would be on the machines you own to create a separate user just for university work. Not just for assignments, but also for web browsing, and such; also email if you've got a separate email for uni. Keeps everything related to uni in a single place and doesn't get intertwined with your personal stuff. Also makes it easier to stay focused, by blocking time sinks (social media, games, etc.) – datenwolf Jul 08 '22 at 07:49

1 Answers1

4

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

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79