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 Unused Includes inspection](https://resources.jetbrains.com.cn/help/img/idea/2024.2/cl_unused_includes.png)
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
Go to C/C++, Unused Code, Unused Include Directive in the list of inspections.
and navigate toIn 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.
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).