Main Content

Useless include

An include directive is present but not used

Since R2022b

Description

This defect occurs when you do not use an included header.

For instance, 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.

If an object is used inside a nested macro, Polyspace can miss the usage. As a result, Polyspace might flag the include that declares the object as USELESS_INCLUDE.

Risk

Unnecessary file dependencies slow the build speed of the project. To improve build speed, remove include directives that are not used.

Fix

Remove the unused include directive.

Examples

expand all

This example does not use the <cmath> and <stdexcedpt> header files or their include directives.

#include <iostream>      
#include <cmath>        
#include <stdexcept>    
#include <vector>       

int age = 0;
std::vector<int> allAges;

void saveAge()
{
	std::cout << "Enter age: ";
	std::cin >> age;
	
	allAges.push_back(age);
}

int main() {
	while (age <= 40)
	{
		saveAge();
	}

	for (int x : allAges)
	{
		std::cout << x << std::endl;
	}

	return 0;
}
Correction — Remove Unused Include Directives

One correction is to delete the unused include directives from the file.

#include <iostream>
#include <vector>

int age = 0;
std::vector<int> allAges;

void saveAge()
{
	std::cout << "Enter age: ";
	std::cin >> age;
	
	allAges.push_back(age);
}

int main() {
	while (age <= 40)
	{
		saveAge();
	}

	for (int x : allAges)
	{
		std::cout << x << std::endl;
	}

	return 0;
}

Result Information

Group: Good Practice
Language: C | C++
Default: Off
Command-Line Syntax: USELESS_INCLUDE
Impact: Low

Version History

Introduced in R2022b

expand all