Main Content

MISRA C++:2023 Rule 0.2.2

A named function parameter shall be used at least once

Since R2024b

Description

Rule Definition

A named function parameter shall be used at least once

Rationale

Unused parameters of a function can indicate programming errors or incomplete code. For instance, the parameter might be intended for an operation that you forgot to code.

Polyspace Implementation

The rule checker reports violations on unnamed unused parameters of a function except in the following cases:

  • The function has an empty body.

  • The function contains Assembly language instructions (which might be using the parameters).

  • The parameters are declared with the attribute [[maybe_unused]].

If a rule violation is reported, the result details for the violation shows which function parameters are unused.

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>

std::uint32_t foo(std::uint32_t param1,                   // Compliant
                  std::uint32_t param2,                  // Noncompliant
                  std::uint32_t param3) {                // Noncompliant
    return param1;
}

std::uint32_t bar(std::uint32_t param1,                    // Compliant
                  std::uint32_t param2 [[maybe_unused]]) { // Compliant
#ifdef MACRO_TO_ENABLE_PARAM2
    param1 = param1 > param2? param1: param2;
#endif
    return param1;
}

In this example, the function foo() violates the rule while the function bar() complies with the rule.

  • The function foo() has two unused parameters, param1 and param2.

  • The function bar() has a parameter param2, which is unused if the macro MACRO_TO_ENABLE_PARAM2 is not defined. However, the parameter has been declared with the attribute [[maybe_unused]] to comply with the rule.

Check Information

Group: Language independent issues
Category: Required

Version History

Introduced in R2024b