doc/manual/source/development/static-analysis.md
Nix uses clang-tidy for static analysis of C++ code. This helps catch bugs, enforce coding standards, and maintain code quality.
To run clang-tidy on the entire codebase in the development shell:
$ nix develop .#native-clangStdenv
$ configurePhase
$ meson compile -C build clang-tidy
This will analyze all C++ source files and report any warnings.
To automatically apply fixes for certain warnings:
$ meson compile -C build clang-tidy-fix
Warning
Review the changes before committing, as automatic fixes may not always be correct.
clang-tidy runs automatically on every pull request via GitHub Actions.
The CI job builds .#hydraJobs.clangTidy.x86_64-linux which:
The clang-tidy configuration is in .clang-tidy at the repository root (symlinked from nix-meson-build-support/common/clang-tidy/.clang-tidy).
If a warning is a false positive, you can suppress it in several ways:
Inline suppression (preferred for specific cases):
// NOLINTBEGIN(bugprone-some-check)
... code ...
// NOLINTEND(bugprone-some-check)
Or for a single line:
int x = something(); // NOLINT(bugprone-some-check)
Configuration file (for project-wide suppression):
Add the check to the disabled list in .clang-tidy:
Checks:
- -bugprone-some-check # Reason for disabling
Check options (for configuring check behavior):
CheckOptions:
bugprone-reserved-identifier.AllowedIdentifiers: '__some_identifier'
To enable additional checks:
nix-meson-build-support/common/clang-tidy/.clang-tidyChecks listThe Nix project includes infrastructure for custom clang-tidy checks in src/clang-tidy-plugin/.
These checks can enforce Nix-specific coding patterns that aren't covered by standard clang-tidy checks.
To add a new custom check:
src/clang-tidy-plugin/nix-clang-tidy-checks.cc.clang-tidy with the nix- prefix