Main Content

MISRA C++:2008 Rule 0-1-12

There shall be no unused parameters (named or unnamed) in the set of parameters for a virtual function and all the functions that override it

Description

Rule Definition

There shall be no unused parameters (named or unnamed) in the set of parameters for a virtual function and all the functions that override it.

Rationale

Unused parameters often indicate later design changes. You perhaps removed all uses of a specific parameter but forgot to remove the parameter from the parameter list.

Unused parameters constitute an unnecessary overhead. You can also inadvertently call the function with a different number of arguments causing a parameter mismatch.

Polyspace Implementation

For each virtual function, the checker looks at all overrides of the function. If an override has a named parameter that is not used, the checker shows a violation on the original virtual function and lists the override as a supporting event.

Note that Polyspace checks for unused parameters in virtual functions within single translation units. For instance, if a base class contains a virtual method with an unused parameter but the derived class implementation of the method uses that parameter, the rule is not violated. However, if the base class and derived class are defined in different files, the checker, which operates file by file, flags a violation of this rule on the base class.

The checker does not flag unused parameters in functions with empty bodies.

Additional Message in Report

There shall be no unused parameters (named or unnamed) in the set of parameters for a virtual function and all the functions that override it.

Function funcName has unused parameters.

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

class base {
    public:
       virtual void assignVal (int arg1, int arg2) = 0; //Noncompliant
       virtual void assignAnotherVal (int arg1, int arg2) = 0;
};

class derived1: public base {
    public:
       virtual void assignVal (int arg1, int arg2) {
           arg1 = 0;
       }
       virtual void assignAnotherVal (int arg1, int arg2) {
           arg1 = 1;
       }
};

class derived2: public base {
    public:
       virtual void assignVal (int arg1, int arg2) {
           arg1 = 0;
       }
       virtual void assignAnotherVal (int arg1, int arg2) {
           arg2 = 1;
       }
};

In this example, the second parameter of the virtual method assignVal is not used in any of the derived class implementations of the method.

On the other hand, the implementation of the virtual method assignAnotherVal in derived class derived1 uses the first parameter of the method. The implementation in derived2 uses the second parameter. Both parameters of assignAnotherVal are used and therefore the virtual method does not violate the rule.

Check Information

Group: Language Independent Issues
Category: Required

Version History

Introduced in R2016b