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:
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
Configure the inspection
Go to C/C++, Unused Code, Unused Include Directive in the list of inspections.and navigate to
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
#includedirectives 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
#includesdirectives with the declarations not used in the current file directly.
Detect not required: the most aggressive strategy which detects the minimal subset of the
#includedirectives to keep the file compilable and suggests removing all the rest.
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).