CLion 2021.3 Help

Inspection: 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.

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: 09 July 2021