Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

Static analysis with clang-tidy

Use clang-tidy and other static analyzers to find bugs and modernize C++ code: check groups, configuration, compile_commands.json and before/after fixes.

A static analyzer reads your source code without running it and reports mistakes, risky patterns and outdated style. clang-tidy is the most popular one for C++. It uses the Clang front end, so it understands real C++ (templates, overloads, the standard library), and many of its checks can rewrite your code automatically.

Static analysis complements the other tools. Compiler warnings are fast and always on, sanitizers find bugs that actually happen when you run, and clang-tidy looks for classes of bugs and style problems across all code paths, including ones your tests never reach.

Terminal
clang-tidy src/main.cpp -p build --checks='-*,modernize-*,bugprone-*' --fix

clang-tidy is not installed on our runner, so the tool output on this page is read-only. The live examples show pairs of code, "before" as a check would flag it and "after" as it would be fixed, and verify that both versions behave the same way.

Try it

C++ modernize-use-nullptr, use-auto and use-override

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ performance and bugprone fixes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

What clang-tidy checks

Checks are grouped by prefix. Enable groups in a .clang-tidy file at the project root:

YAML
Checks: >
  -*,
  bugprone-*,
  clang-analyzer-*,
  cppcoreguidelines-*,
  modernize-*,
  performance-*,
  readability-*,
  -modernize-use-trailing-return-type,
  -readability-magic-numbers
WarningsAsErrors: 'bugprone-*'
HeaderFilterRegex: 'include/.*'
Prefix Looks for
bugprone- Likely mistakes: use after move, suspicious string concatenation, swapped arguments, dangling handles.
clang-analyzer- Path-sensitive analysis: null dereference, leaks, use after free, uninitialized values.
cppcoreguidelines- Rules from the C++ Core Guidelines.
modernize- Old constructs replaced by modern ones (nullptr, override, auto, range-for, make_unique).
performance- Needless copies, std::endl, missing reserve, pass-by-value of large objects.
readability- Naming, braces, empty() over size()==0, simplifiable conditions.
misc- Unused parameters, non-private data members, and others.

Running it

clang-tidy needs to know how each file is compiled. Generate a compilation database with CMake and point the tool at it:

Terminal
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
run-clang-tidy -p build -quiet            # all files in parallel
clang-tidy -p build src/a.cpp --fix       # one file, apply automatic fixes

A typical report looks like this:

TEXT
src/a.cpp:12:5: warning: use nullptr [modernize-use-nullptr]
   12 |     int* p = NULL;
      |              ^~~~
      |              nullptr

Other tools

  • Compiler warnings (-Wall -Wextra, -Wshadow, -Wconversion) and GCC's -fanalyzer for flow analysis of C code.
  • cppcheck is a separate analyzer with few false positives and no compilation database needed.
  • clang-format formats code (it does not analyze it); keep it separate from tidy.
  • include-what-you-use checks that you include exactly the headers you use.
  • MSVC /analyze and the Visual Studio Code Analysis for Windows projects.

Usage notes

  • Start small. Enabling every check on a large project drowns you in noise. Add one group at a time and fix it before moving on.
  • Review automatic fixes. --fix is safe for simple checks (use-nullptr, use-override), but check the diff for others, and run the tests.
  • Suppress deliberately. Use // NOLINT(check-name) on one line or NOLINTNEXTLINE, with a short reason. Do not disable a whole check because one line is a false positive.
  • Run in CI on changed files, and treat only the bugprone- family as errors, so style checks never block an urgent fix.
  • Match the compiler. Analysis needs the same -std= and include paths as the build, which the compilation database provides. Missing headers cause confusing errors.
  • A clean report is not proof of correctness. Static analysis has false positives and false negatives; combine it with tests and sanitizers.