In this section:

Overview

Insure++ can detect unused variables in your code. Since these are not normally errors, but informative messages, this category is disabled by default.

Additional configuration required

 In order for Insure++ to detect unused variables, the suppressEDGWarning off option is required.

Insure++ distinguishes between unused variables that have been assigned and variables that are unassigned.

CodeDescriptionEnabledReportedPlatform
UNUSED_VARUnused variables(error)CompilationWindows/Unix
(assigned)Variable is assigned but never used(error)CompilationWindows/Unix
(unassigned)Variable is never used(error)CompilationWindows/Unix


Problem 1

The following code assigns a value to the variable max but never uses it.

/*
 * File: unuasign.c
 */
main()
{
	int i, a[10];
	int max;

	a[0] = 1;
	a[1] = 1;
	for(i=2; i<10; i++)
	a[i] = a[i-1]+a[i-2];
	max = a[9];
}

Diagnosis During Compilation

The following code will normally run without displaying a message. If UNUSED_VAR messages are enabled, however, the following display will result.

[unuasign.c:7] **UNUSED_VAR(assigned)**
	Unused variable: max
>>		 int max;

---- Associated Common Weakness Enumerations ----
CWE-563: Assignment to variable without use
CWE-1164: Irrelevant code
  • Line 1: Source line at which the problem was detected.
  • Line 2: Description of the error and the parameters used.
  • Line 5-7: CWEs associated with this problem.

Problem 2

The following code never uses the variable max.

/*
 * File: unuvar.c
 */
main()
{
	int i, a[10];
	int max;

	a[0] = 1;
	a[1] = 1;
	for(i=2; i<10; i++)
	a[i] = a[i-1]+a[i-2];
}

Diagnosis at Runtime

If UNUSED_VAR messages are enabled, the following display will result.

[unuvar.c:7] **UNUSED_VAR(unused)**
	Unused variable: max
>>		 int max;

---- Associated Common Weakness Enumerations ----
CWE-1164: Irrelevant code
  • Line 1: Source line at which the problem was detected.
  • Line 2: Description of the error and the parameters used.
  • Line 5-6: CWE associated with this problem.

Repair

These messages are normally suppressed but can be enabled by unsuppressing UNUSED_VAR.

In most cases, the corrective action to be taken is to remove the offending statement because it is not affecting the behavior of the application. In certain circumstances, these errors might denote logical program errors in that a variable should have been used but wasn’t.

References

The table below shows Common Weakness Enumerations associated with this error.

CWEDescription
CWE-563Assignment to variable without use
CWE-1164Irrelevant code
  • No labels