Main Content

MISRA C:2012 Rule 2.8

A project should not contain unused object definitions

Since R2024b

Description

Rule Definition

A project should not contain unused object definitions.1

Rationale

An object is unused if the program compiles after removing the definitions and declarations of the object. Such unused objects can be confusing because it is not clear whether such objects are redundant or left unused by mistake.

Polyspace Implementation

Polyspace® reports violations on unused variables with internal linkage, such as:

  • Unused local variables

  • Unused global variables declared static

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

The rule violation is reported on the variable declaration. Polyspace does not report violations on unused function parameters.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

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

#include <stdint.h>

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

int16_t getSensorValues() {
    return sensor2;
}

Check Information

Group: Unused code
Category: Advisory
AGC Category: Advisory

Version History

Introduced in R2024b


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.