Mistake on this page? Email us

Code linting

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.

Enabling clang-tidy

Clang-tidy is not enabled by default and can be enabled from the preferences.

To enable clang-tidy:

  1. Go to File > Settings > Open Preferences.

  2. 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).

  3. 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 .clang-tidy files.

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 clang-analyzer-cplusplus* ones:

-*,clang-analyzer-*,-clang-analyzer-cplusplus*

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:

  1. Check that the Clang Tidy preference is enabled.

  2. From the Explorer view, go to the active program and create a .clang-tidy file in the root folder of the program.

  3. Add checks as follows:

    In this example, we enable all the checks and provide the additional option to modernize-use-nullptr:

    Checks: '*'
    CheckOptions:
    - key:             modernize-use-nullptr.NullMacros
    value:           NULL,CUSTOM_NULL
    

See the official YAML website for more details.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.