Main Content

MISRA C++:2023 Rule 0.2.4

Functions with limited visibility should be used at least once

Since R2024b

Description

Rule Definition

Functions with limited visibility should be used at least once

Rationale

Functions with limited visibility are typically not meant to be called in later extensions of the current API. If such a function is left unused, it is most likely not a deliberate choice and probably indicates a programming error or an issue with the software design.

Polyspace Implementation

The rule checker reports violations on functions with limited visibility that are defined but not called within the scope where the function is visible. In particular, following the MISRA C++:2023 specifications, the rule checker reports:

  • Unused class member functions that are defined as private and not virtual. The checker makes an exception for the special member functions and member functions declared as =delete.

  • Unused unctions defined in an anonymous namespace, or defined in a class inside an anonymous namespace.

  • Unused functions defined as static in a named namespace or outside a namespace.

The checker does not report violations on:

  • Unused functions if the function declarations use the [[maybe_unused]] attribute.

  • static inline functions.

For overloaded functions, the checker does not report any of the overloads as long as one function in the overload set is called.

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>
uint32_t reg0;

namespace {
    void set(uint32_t val) {  // Compliant
        reg0 = val;
    }
    void reset(void) {       // Noncompliant
        reg0 = 0;
    }
}

void setRegisters(uint32_t registerVal) {
    set(registerVal);
}

In this example, the function reset() is noncompliant since it is defined in an anonymous namespace but not used anywhere within the same translation unit.

Check Information

Group: Language independent issues
Category: Advisory

Version History

Introduced in R2024b