CLion constantly monitors your code for potential errors. If it finds anything, it highlights the suspicious piece of code in the editor. If you look in the right-hand editor gutter, you will see yellow and red error strips that, if clicked, navigate you to the detected issues. Another way to navigate from one highlighted issue to another is by pressing F2/Shift+F2. The status indicator at the top of the gutter summarizes the file status.
In addition to finding compilation errors, CLion identifies code inefficiencies and even performs data flow analysis over your code to locate unreachable/unused code, as well as other issues and 'code smells'.
CLion’s on-the-fly code inspections cover about 40 potential problem cases in C/C++ code and quite a few for other languages as well.
When a problem is highlighted, place the caret on it, press Alt+Enter and choose from the suggested quick-fix solutions. (Alternatively, enter the context menu by clicking the light bulb next to the line.)
You can also choose to fix all similar issues across your project. Or, if you don’t find this inspection useful, you can suppress it for the desired scope.
CLion provides detailed descriptions for all of the available inspections. You can also manage their severity (choose from Typo, Warning, Error, etc.) or even in some cases adjust an inspection’s parameters to better reflect your requirements.
You can run multiple inspections (or even all of them) in this batch mode with
| Inspect Code.
If you’d like to eliminate a particular problem from your entire code base, you can use Run inspection by name (Ctrl+Alt+Shift+I) and select the desired scope. A separate window with inspection results will open in which you can regroup issues and batch-apply quick-fixes to all the issues, where possible.
There are several inspections implemented on top of the custom Clangd-based engine in CLion:
The ‘unused includes’ check suggests 3 detection strategies: a conservative one, an aggressive one, and the default one (Detect not directly used) which is the closest to the “Include What You Use” principle.Learn more
CLion comes with the Clang-Tidy integration. Clang-Tidy checks are shown the same way as CLion’s own built-in code inspections, and quick-fixes are also available via Alt+Enter.
Go to Settings/Preferences | Editor | Inspections | C/C++ | General | Clang-Tidy to tune the list of checks enabled/disabled in CLion. Clang-Tidy command line format is used in the text field. The default configuration can be found here. Or use .clang-tidy config files instead of the IDE-provided settings.
Besides, individual checks can be enabled/disable via a context menu.
Enable C++ Core Guidelines or Clang Static Analyzer checks, try Modernize checks or even implement your own checks and get them immediately in CLion (for custom checks, change the Clang-Tidy binary to your own one under Settings/Preferences | Languages & Frameworks | C/C++).