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 1

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


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.