AutosarC++18_10-A16.2.2¶
There shall be no unused include directives
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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 <algorithm>. It is not a violation of this rule if <algorithm>, 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 isused
#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
#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.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
unused_include |
#include {inc} can be removed as file does not use anything from it |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
accept_incomplete_arguments¶
accept_incomplete_arguments
Names of templates for which incomplete types should be accepted as allowed template type arguments (so forward declarations of these arguments are enough to instantiate the template).Type: list[bauhaus.analysis.config.ShortTypeName]
Default:
['shared_ptr', 'unique_ptr', 'weak_ptr', 'auto_ptr', 'default_delete', 'enable_shared_from_this', 'list', 'forward_list', 'vector', 'allocator', 'polymorphic_allocator', 'is_void', 'is_null_pointer', 'is_integral', 'is_floating_point', 'is_array', 'is_pointer', 'is_lvalue_reference', 'is_rvalue_reference', 'is_member_object_pointer', 'is_member_function_pointer', 'is_enum', 'is_union', 'is_class', 'is_function', 'is_reference', 'is_arithmetic', 'is_fundamental', 'is_object', 'is_scalar', 'is_compound', 'is_member_pointer', 'is_const', 'is_volatile', 'is_signed', 'is_unsigned', 'rank', 'extent', 'is_same', 'remove_const', 'remove_volatile', 'remove_cv', 'add_const', 'add_volatile', 'add_cv', 'remove_reference', 'add_lvalue_reference', 'add_rvalue_reference', 'make_signed', 'make_unsigned', 'remove_extent', 'remove_all_extents', 'remove_pointer', 'add_pointer', 'decay', 'enable_if', 'conditional']
avoid_uncertain_messages¶
avoid_uncertain_messages : bool = True
context_macros¶
context_macros : list[str] = []
enums_can_be_redeclared¶
enums_can_be_redeclared : bool = False
#includes.
interface_headers¶
interface_headers : list[bauhaus.analysis.config.FileGlobPattern] = []
#include files without using them. #includes to these
interface files are also treated as ok.
limit_for_moving¶
limit_for_moving : int = 3
limit_for_replacing¶
limit_for_replacing : int = 3
routines_can_be_redeclared¶
routines_can_be_redeclared : bool = False
#includes.
typedefs_can_be_redeclared¶
typedefs_can_be_redeclared : bool = False
#includes.