Main Content

AUTOSAR C++14 Rule A16-2-2

There shall be no unused include directives

Since R2021b

Description

Rule Definition

There shall be no unused include directives.

Rationale

Unused include directives unnecessarily increase the code base size and introduce dependencies, which slows down the compilation process. To avoid this issue, remove unused include directives.

Remove unused include directives from the code.

Polyspace Implementation

Polyspace® reports a violation when a source file includes a header file that contains declarations of symbols that are not used in the source file. This rule checker does not check any external libraries.

Polyspace considers an include directive to be unused when you do not:

  • Invoke macros defined in the included file or the included file's includes.

  • Use data types defined in the included file or the included file's includes.

  • Call or use references to an object or function defined in the included file or the included file's includes.

  • Use an externally visible object provided by the included file outside of the file.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

Because the source file does not use a symbol, macro, or data type defined in the <algorithm> header file, Polyspace reports a violation on the <algorithm> header file. Remove the unused include directive from the source file.

#include <algorithm>	//Noncompliant
#include <cstdint>
#include <iostream>


void main()
{
	int32_t num = 5;
	std::cout << "Hello world : " << num;
}

Check Information

Group: Preprocessing directives
Category: Required, Automated

Version History

Introduced in R2021b