In one of the articles here on the blog we published about clang-format, today we are going to know another Clang/LLVM application collection tool: clang-tidy
.
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.
To install 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:
main.cpp
Even if you compile with the proper parameters of your compiler to detect failures, you will not be able to detect the
warning
.
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 allclang-analyzer-*
exceptclang-analyzer-cplusplus*
.
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.
clang-tidy
: