Main Content

MISRA C++:2023 Rule 0.2.3

Types with limited visibility should be used at least once

Since R2024b

Description

Rule Definition

Types with limited visibility should be used at least once

Rationale

If you declare a data type but leave the type unused, it is unclear to another developer or a reviewer if the type is left unused by mistake.

Polyspace Implementation

The rule checker reports a violation if a data type declared in block scope is not used within the block, or if a data type declared in an unnamed namespace is not used within the same translation unit. Based on the MISRA™ C++:2023 specifications, the following are considered as usage:

  • The closure type of a lambda expression is always considered as used.

  • A type is considered as considered as used only if it is used outside its own definition. Note that:

    • The definition of a type includes the definitions of its members and hidden friends.

    • The partial and explicit specializations of a template class are considered as part of the definition.

  • An enumeration type is considered as used if any of its enumerators are used.

  • An anonymous union is considered as used if any of its members are used.

Following the MISRA C++:2023 specifications, the rule checker ignores types declared with the [[maybe_unused]] attribute, template parameters, and partial and explicit specializations of templates.

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

namespace {
    class record { // Noncompliant
    };
    class item {   // Compliant
    };
}

void useItem(class item anItem) {
    
}

In this example, the checker reports a violation on the type class record because the type is not used (considering the above content to be the entirety of the translation unit).

Check Information

Group: Language independent issues
Category: Advisory

Version History

Introduced in R2024b