Clang-Tidy is a powerful open-source code analysis tool based on the popular Clang compiler. It comes with an extensive set of code checks and accompanying fixes for typical programming errors.
ReSharper provides seamless integration with Clang-Tidy. This means that as you edit a file, ReSharper runs Clang-Tidy on it in background as a separate process, with inspection results shown alongside other ReSharper's inspections. Unsaved files are handled transparently by saving the text of the current document to a temporary file and instructing Clang-Tidy to use it instead of the on-disk version.
To distinguish between Clang-Tidy checks and ReSharper’s inspections, you can look at the name of the check in square brackets, appended to the inspection message:
In addition to diagnostics from Clang-Tidy checks, ReSharper also issues diagnostics from Clang Static Analyzer and warnings provided by the Clang compiler itself. All of them have corresponding configurable severity levels, which can be adjusted to meet your needs.
Some of the Clang-Tidy checks that duplicate ReSharper’s built-in inspections are turned off by default. In addition, all static analyzer checks are disabled, since enabling them significantly slows down Clang-Tidy.
As with the built-in inspections, you can use the inspection context menu to quickly change the severity of a Clang-Tidy check, suppress it via a comment (in specific scope or globally), or find and investigate all similar issues. There is also an additional action that opens the online documentation page for a particular check.
ReSharper lets you apply the fixes provided by Clang-Tidy similarly to its own built-in quick-fixes. As with other bulk fixes, Clang-Tidy fixes can be applied individually or inside a specific scope (file, folder, project, or solution).
By default, ReSharper will reformat code changed by Clang-Tidy fixes to maintain the code style. This behavior can be disabled via the Reformat changed code after applying Clang-Tidy fix-its setting on the page of ReSharper options (Alt+R,,O). Disabling this setting will speed up the execution of Clang-Tidy fixes.
Applying Clang-Tidy fixes with code cleanup
Clang-Tidy fixes can also be applied with code cleanup. Built-in code cleanup profiles do not include the Clang-Tidy stage, so to enable it you need to create a new custom profile and select the Clang-Tidy checks, which will be executed when you run code cleanup with this profile.
The selected fixes will be applied even if the corresponding inspections are turned off on the page of ReSharper options (Alt+R,,O). Be careful when enabling too many checks at once — code cleanup runs Clang-Tidy only once for a given file, so if multiple fixes touch the same block of code, the changes might conflict with each other.
ReSharper does not write its own Clang-Tidy config, so a user-provided one can be picked. Clang-Tidy attempts to read configuration for each source file from a
.clang-tidy file located in the closest parent directory of the source file. The source file here (as with the rest of ReSharper inspections) is the
.cpp file itself if the analysis is run on it, or a random
.cpp file which includes the header when the analysis is performed on a header file. Alternatively, you can select the Use a specific clang-tidy configuration file setting on the page of ReSharper options (Alt+R,,O) to specify the location of configuration file.
-checks command line option of the Clang-Tidy binary is used to pass the list of enabled checks, which then gets appended to the value of the checks option read from the Clang-Tidy config. To construct the
-checks argument, ReSharper takes the value of the List of enabled/disabled Clang-Tidy checks setting on the page of ReSharper options (Alt+R,,O) and appends to it the names of checks which are turned off on the options page.
By default, ReSharper uses built-in binary of Clang-Tidy 9. You can make ReSharper use any other Clang-Tidy binary (version 7 or later). To do so, you have two additional options on the page of ReSharper options (Alt+R,,O):
Find in %PATH% - Use Clang-Tidy found in the location specified in the
Custom - Use a specific Clang-Tidy executable.
If necessary, you can also disable Clang-Tidy analysis in the current document by pressing Ctrl+Shift+Alt+9 or through the Enable Clang-Tidy analysis toggle in the status indicator context menu.
Limitations of Clang-Tidy integration
Files using UTF-16 or UTF-32 encoding cannot be read by Clang and thus are not supported by Clang-Tidy at the moment.
Sometimes Clang-Tidy might crash on your code, and Windows will show you the “Process has stopped working” message. In this case disable Clang-Tidy integration for the current solution on the page of ReSharper options (Alt+R,,O).