Linting is the automated checking of your source code for programmatic and stylistic errors. It is intended to improve the overall quality of your code and make code reviews and tests more efficient.
Mbed Studio uses clang-tidy, a clang-based C/C++ linter tool. Clang-tidy is an extensible framework for diagnosing typical programming errors, or style issues (generally anything which can be detected during static analysis of the code). It checks your code as you type, using squiggly lines to highlight problems and light bulbs to suggest fixes in the editor. The clang-tidy linter checks complement existing IntelliSense capabilities that were already available with Mbed Studio and brought by clangd.
Clang-tidy is not enabled by default and can be enabled from the preferences.
To enable clang-tidy:
Go to File > Settings > Open Preferences.
Select the User or the Workspace tab. If you have several workspaces and want to define preferences by workspace, make sure you have selected the correct workspace first (use File > Open Recent Workspace... to switch between workspaces).
Search for the Clang Tidy preference and select the checkbox to enable C/C++ linting.
Configuring clang-tidy checks
You can decide which clang-tidy checks you want to enable or disable. You can also configure advanced custom checks using
Configuring checks from the preferences
The Clang Tidy Checks preference allows you to configure the checks that clang-tidy should carry out. This preference is only enabled when the Clang Tidy preference is enabled too. Note that the checks are activated for all your programs.
All default checks are enabled by default as follows:
To configure checks from the preferences:
If you need to enable or disable some particular checks, use the clang-tidy command-line format in the Clang Tidy Checks field. It specifies a comma-separated list of positive and negative globs: positive globs add subsets of checks, while negative globs (prefixed with
-) remove them.
For example, the following command line will disable all default checks
-* and enable all the
clang-analyzer-* checks except for the
See the Clang-Tidy Checks page for a complete list of available checks. Supported checks are indicated by a "Yes" in the Offers fixes column. See also the Clang-Tidy page for more general information.
Configuring advanced custom checks
Further configuration is possible using
.clang-tidy files in YAML format. There can be one file per program, in the root folder of each program. The
.clang-tidy file of the active program takes precedence over what you have set in the preferences.
For programs configured for source control, one advantage of using
.clang-tidy files is that the files you create can be shared and updated in a collaborative way.
To configure custom checks:
Check that the Clang Tidy preference is enabled.
From the Explorer view, go to the active program and create a
.clang-tidyfile in the root folder of the program.
Add checks as follows:
In this example, we enable all the checks and provide the additional option to
Checks: '*' CheckOptions: - key: modernize-use-nullptr.NullMacros value: NULL,CUSTOM_NULL
See the official YAML website for more details.