How to use clang-tidy

clang-tidy is a LLVM tool that can be used as a static checker on your C++ codebase and to fix errors it finds. A full list of current checks and their descriptions can be found here. The number of checks available to you will depend on the clang-tidy version you are using.

  • Installing this tool is easy:
$ sudo apt install clang-tidy

This typically installs a stable but older version of the tool with less number of checks.

  • To list the checks it performs by default:
$ clang-tidy -list-checks

On my computer I found it had 80 checks enabled by default.

  • To list all the checks that it can perform:
$ clang-tidy -list-checks -checks="*"

On my computer I found that it had a total of 292 checks.

  • To check a file using all checks enabled by default:
$ clang-tidy foobar.cpp
  • To check a file using all checks enabled by default and fix the errors using suggested fixes:
$ clang-tidy -fix foobar.cpp
$ clang-tidy -fix-errors foobar.cpp
  • To use a specific check, say modernize-use-nullptr:
$ clang-tidy -checks="-*,modernize-use-nullptr" foobar.cpp

Tried with: clang-tidy 6.0.0 and Ubuntu 18.04

Advertisements