Find Unused Includes

The Find Unused Includes algorithm analyzes either a whole C++ project or a single source file to find all #include-statements which are not necessarily required.
Consider the following example:

#include <vector>
#include <string>
int main() {
    std::string s;
    return 0;
}

Clearly the first include statement is not needed here. When running the Find Unused Includes algorithm on the sample code it will suggest the removal of the first include as shown in the following screenshot.

Types of Unused Includes

Includator recognizes three distinct cases of unnecessary include directives:
  • Duplicate Includes
  • Unused Includes
  • Covered Includes
    Below we will give a short explanation of the three cases including an example for illustration.

Duplicate Includes

If for any reason the same include directive has been placed into a source file twice, Includator recognizes this and reports the second (and any further if there are more similar includes) as duplicate.

Example

#include <string>
#include <string>
int main() {
    std::string s;
}

Running Find Unused Includes on the source code above creates a suggestion to remove the second #include <string> directive. The screen shot below shows the corresponding suggestion dialog:

Checking the suggestion to remove the include and clicking OK removes the include directive. An information marker remains there to indicate where the include directive has been removed:

Unused Includes

Includator recognizes if an include directive does not provide a declaration or definition of any symbol in the source file analyzed.

Example

The example from the beginning of this page belongs to this category. We have an include directive for vector but do not use std::vector at all.

#include <vector>
#include <string>
int main() {
    std::string s;
    return 0;
}

After running Find Unused Includes Includator shows the following dialog, indicating that there is no reference to any symbol defined (or declared) in the header file vector:

Covered Includes

It is also possible for Includator to recognize if all symbols declared/defined by an include directive are already covered by other include directives. This usually happens when an include directive transitively includes another file which is also included directly by the target source file.

Example

In the file below we have two include directives in main.cpp. Both provide symbols which are referenced in the main function. Actually, #include “Shape.h” is not required from the compilers point of view.

main.cpp:

#include "Shape.h" 
#include "Square.h" 
int main() {
    Shape* s = new Square();
    return 0;
}

Shape.h:

#ifndef SHAPE_H
#define SHAPE_H
struct Shape {
    virtual ~Shape();
};
#endif

Square.h:

#ifndef SQUARE_H
#define SQUARE_H
#include "Shape.h" 
struct Square : Shape {
    unsigned side;
};
#endif

Includator recognizes that the code is compilable without the inclusion of Shape in main.cpp. It suggests this directive to be removed and indicates that the symbols of Shape.h are covered through #include “Square.h”.

Disable Covered Includes

While it makes sense to have the block of include directives lean, there might be coding guidelines obliging to have an include directive for every symbol used in a source file. Therefore, the removal suggestion of otherwise covered includes is an optional feature which can be disabled on Includator’s preference page.