CLion 2019.2 Help

Inspection in Detail: Unused Include Directive

Needless header files included in your code slow down the compilation and can pollute global namespace or even cause prepossessing collisions. Such includes add unnecessary dependencies, making your code harder to explore and maintain (for example, you will see a lot of irrelevant results in code completion).

The Unused Include Directive inspection detects the #include directives that are not required and can be safely removed:

Unused Includes inspection

The inspection does not warn you about the includes in source files with compilation errors. Also, it checks only the headers that contain #pragma once or header guards - files with none of them are always marked as used.

The inspection is enabled by default when the Clangd-based language engine is turned on. If you disable the Clangd engine, Unused Include Directive will still work, but false positives are possible in this case, and the chosen strategy will not affect the inspection's behavior: when working without Clangd, it suggests removing the maximum number of unused directives (the Detect not required option). Also, in this case, the inspection checks all the headers regardless of whether or not they have header guards or #pragma once.

Configure the inspection

  1. Go to Settings / Preferences | Editor | Inspections and navigate to C/C++, Unused Code, Unused Include Directive in the list of inspections.

    Unused Includes settings

  2. In the Options field, select the diagnostic strategy for detecting and removing unused includes:

    • Detect completely unused: the most conservative strategy, which detects only the #include directives with the declarations never used in the translation unit.

    • Detect not directly used (default): this strategy follows the Include What You Use principle (the principle that if you use a symbol or type from a header, you should include that header) and detects the #includes directives with the declarations not used in the current file directly.

    • Detect not required: the most aggressive strategy which detects the minimal subset of the #include directives to keep the file compilable and suggests removing all the rest.

  3. If required, set the Run inspection in header files checkbox.

    With this option enabled, the inspection skips umbrella headers, since the directives listed in them are always considered as used (umbrella headers include all the necessary headers at once and don't contain any declarations).

Last modified: 22 August 2019