Contenido principal

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 1

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.

  • Local variables of a function that contains assembly code in an asm block.

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

expand all


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.