Automatically fixing a whole bunch of cppcheck errors

When you start using a certain code check tool for an existing code base, the checker may at first give a lot of warnings about all kind of things. A few days ago, I fired up cppcheck for an existing c++ code base. Cppcheck gave a lot of useful feedback. Problem is however, it can be quite a bit of work to fix all these errors, depending on the size of your project. Luckily, the major part of the warnings belonged to a limited group of error types. This gives us the chance to write a script that automatically fixes most of these errors.

One of the warnings the cppcheck gave is that there should be an explicit keyword before a constructor with a single argument, to avoid it being used in implicit type conversions. If the cppcheck errors are in errors.txt, we can use the following python code in combination with vim to automatically fix this.


This is notably slow, and it took me quite some fiddling to get vim to do what I want (read: figuring out these weird command line arguments). I wanted to do the next thing using fate, an experimental text processing engine that I’ve been working on. This next thing turned out to be passing const strings by reference, for reasons of performance. The following code fixes these warnings.

Fate is notably faster in this usecase than vim, probably because it is more lightweight. At the time of writing, fate is still very experimental, but if you like you can have a look at it and fiddle around.