How to make clang-tidy ignore 255


Published on 2023-04-20 by Kenneth Flak

Back to Tech Research


clang-tidy is an absolutely fantastic tool for checking your c++ code, especially for a rookie programmer like myself. It is, however, sometimes a bit on the over-zealous side, especially when it comes to flagging magic numbers in the code. Don't get me wrong, I'm all in favor of getting rid of magic numbers. I've spent far too much time perusing my own and other's code trying to figure out what a specific number means in a complex chain of logic. If the author of the code would have just named the goddamn thing, life would've been so much easier!

However, there are times when you just want to write 255 in your code and be done with it once and for all. Especially when writing rgb(a) values in your openFrameworks app, where everybody knows that the max value of any color is going to be 255.

So.

Last night I spent a couple of hours trying to get clang-tidy to stop bothering me about this. First of all: clang-tidy uses yaml for its configuration needs. Fine. Who am I to judge. Except, if your whitespace is off by a single character, clang-tidy dies.

Worse still, if you try to figure out what the documentation actually means when it says:

Semicolon-separated list of magic positive integers

then you have to figure out what the hell is meant by a 'list'. Because this is not at all clear. A short trip around the webs and its various yaml tutorials tells me that a list in yamlesque looks like this:

value: 
  - listItem1
  - listItem2
  - listItem3
  - listItem4

or, more succinctly:

value: [listItem1,listItem2, listItem3, listItem4]

However, the clang-tidy docs just told me to use a semicolon-separated list of integers...? And then going on to tell me that the default values are {1, 2, 3, 4}. OK... so I try:

  - key:  'readability-magic-numbers.IgnoredIntegerValues'
    value:  '{1; 2; 3; 4; 255}'

Nope. No joy.

OK, what about:

  - key:  'readability-magic-numbers.IgnoredIntegerValues'
    value:  '{1, 2, 3, 4, 255}'

Still not.

At which point I start looking around for other people's configs, to see if someone, somewhere has successfully solved this. I finally stumble on this version:

  - key:  'readability-magic-numbers.IgnoredIntegerValues'
    value:  '1;2;3;4;255'

Yes, someone has actually written this, and I suppose it works for them! Not for me, though. This is the point where I want to dig a hole in the ground and just roll around there for a while. Which, in a sense, I do. I go to bed with the matter unresolved.

Next morning I decide to poke around a bit more, to see what kind of voodoo the nice person that made it work for them has performed, and I notice that they have this in their Checks: section:

Checks:
  -cppcoreguidelines-avoid-magic-numbers,

I add this to my config, and—drumroll—it works! The world is yet again a beautiful and meaningful place, and I can go back to finding other ways of procrastinating (like, for example, writing this blog post)...