Main Content

MISRA C++:2023 Rule 0.2.1

Variables with limited visibility should be used at least once

Since R2024b

Description

Rule Definition

Variables with limited visibility should be used at least once

Rationale

Unused variables take up unnecessary space without contributing to the program output. The presence of unused variables might also indicate a programming error, such as an incorrect variable being used elsewhere.

Polyspace Implementation

The rule checker reports violations on unused variables with internal linkage, for instance:

  • Unused local variables

  • Unused global variables declared static

  • Unused global variables declared outside a header file with a const or constexpr qualifier but not an extern qualifier

  • Unused global variables in an anonymous namespace

The rule violation is reported on the variable declaration.

The checker does not report violations on unused variables of class type with a user-defined constructor or destructor.

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

#include <cstdint>

static int16_t sensor1; // Noncompliant
static int16_t sensor2; // Compliant

int16_t getSensorValues() {
    return sensor2;
}

In this example, the static variable sensor1 has internal linkage but is not used in the file. Therefore, the rule checker reports the declaration of sensor1 as a rule violation.

Check Information

Group: Language independent issues
Category: Advisory

Version History

Introduced in R2024b