Clang-tidy is a tool that performs static code analysis on C++ code, identifying code smells, potential bugs, and other common issues.

$ sudo apt install clang-tidy

Basic usage

Common commands

We can use clang-tidy without a compilation database, but in such cases, it may result in errors.

$ clang-tidy source.cpp         # output report
$ clang-tidy *.cpp > report.txt # save in a file
$ clang-tidy -fix *.cpp         # run automated fixes


Clang-tidy can be configured in a .clang-tidy file.

Checks: >
  -*,                 # remove all
  google-*,           # add all from google
  -google-runtile-int # but remove this one

# WarningsAsErrors: "*"

# Tune some checks
  - { key: name, value: xxx }
  - key: name
    value: xxx


For more complex projects, we generate compile_commands.json which defines the command to compile each file. This file can be generated automatically using CMake:

$ cat compile_commands.json
    "directory": "src",
    "command": "g++ src/source.cpp",
    "file": "source.cpp"
$ clang-tidy -p . [...]

⚠️ If the folder with the compile_commands.json file is different from the current folder (.) and there is no compile_commands.json in the current folder, it will be copied in the current folder.