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;
  • Line 1: Source line at which the problem was detected.
  • Line 2: Description of the error and the parameters used.

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;
  • Line 1: Source line at which the problem was detected.
  • Line 2: Description of the error and the parameters used.

Repair

These messages are normally suppressed but can be enabled by unsuppressing UNUSED_VAR in the Suppressions Control Panel.

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.

  • No labels