How to use clang-tidy for C++
A practical and efficient linter!
Clang-Tidy is a linter for C++. Its purpose is to provide an extensible framework for diagnosing and correcting typical programming errors such as style violations, interface misuse, or bugs that can be deduced through static analysis.
clang-tidy just use the same procedures described in the clang-format article.
The simplest way to check your code with
clang-tidy is: suppose you have the code below that prints a pointer instead of the reference or the data itself:
Even if you compile with the proper parameters of your compiler to detect failures, you will not be able to detect the
Using the command below with
clang-tidy you can detect:
The output will appear:
clang-tidy has its own checks and can also run Clang Static Analyzer checks. Each check has a name and the checks to be performed can be chosen using the
-checks= option, which specifies a list separated by commas and
-, for example:
This command will disable all default checks (
-*) and enable all
In summary, it is a useful tool and used by default in most configurations for LSP.
For more information use
clang-tidy --help and read the official documentation.