How to run clang-tidy with option --fix or --export-fixes

The tool clang-tidy checks code against a vast set of rules, and suggests improvements. In a complex project, however, it is prohibitively difficult to run clang-tidy directly from the command line, since it needs include paths and other options just like a regular compilation command needs. Therefore, it is necessary to run clang-tidy through cmake && make. This can indeed be done, using set(CMAKE_CXX_CLANG_TIDY "clang-tidy") .

With option –fix, clang-tidy can directly correct the sources. Or with option –export-fixes=, it can write suggested fixes in compact form to a YAML file. Again, it is hopeless to do this directly from the command line; we need CMake support. So I attempted set(CMAKE_CXX_CLANG_TIDY "clang-tidy --export-fixes=out.yml") . This failed miserably: Error running 'clang-tidy --export-fixe=out.yml ': No such file or directory.

What’s going wrong? Any workaround?

CMAKE_CXX_CLANG_TIDY is expected to be a list, but you are providing a whitespace separated string. See documentation here and the example here

Thank you, Volker. So my error was the quotation marks in the set command.

In the meantime I found another solution: in the build directory, run cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON (and with set(CMAKE_CXX_CLANG_TIDY clang-tidy in the configuration); then in the source directory run clang-tidy --fix-errors -p=build <source files>.