Rule A16-2-2 (required, implementation, automated)

There shall be no unused include directives.

Rationale

Presence of unused include directives considerably slows down compilation phase, makes the code base larger and introduces unneeded dependencies. Note: In order to determine what an unused include directive is, only the immediate level of includes, and the specifications of external libraries shall be considered. So, for example, if a source file uses the standard library algorithm std::copy, it is required (see also rule A16-2-3) to include the standard library header . It is not a violation of this rule if , possibly through inclusion of other headers, contains declarations of symbols not used in the source file.

Example

// $Id: A16-2-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <algorithm> // Non-compliant - nothing from algorithm header file is used

#include <array>
// Non-compliant - nothing from array header file is used
#include
<cstdint>
// Compliant
- std::int32_t, std::uint8_t are used
#include
<iostream>
// Compliant
- cout is used
#include <stdexcept> // Compliant - out_of_range is used
7 #include <vector>
// Compliant - vector is used
void Fn1() noexcept
{
std::int32_t x = 0;
// ...
std::uint8_t y = 0;
// ...
}
void Fn2() noexcept(false)
{
try
{
std::vector<std::int32_t> v;
// ...
std::uint8_t idx = 3;
std::int32_t value = v.at(idx);
}
catch (std::out_of_range& e)
{
std::cout << e.what() << ā€™\nā€™;
}
}

See also

HIC++ v4.0 [9]: 16.1.5 Include directly the minimum number of headers required for compilation.