Why is this an issue?
Unused parameters are misleading. Whatever the values passed to such parameters, the behavior will be the same.
There are some cases when you want to have an unused parameter (usually because the function has to conform to a fixed prototype, because it is
virtual or it is going to be called from a template). In this case, and if the parameter is never used, an accepted practice is to leave it unnamed.
If it is only sometimes used (for instance, depending on conditional compilation), you may, since C++17, use the [[maybe_unused]]
attribute to be explicit about it.
void f([[maybe_unused]] int i) {
assert(i < 42); // In optimized mode, this assert will be removed, and i will be unused
}
In case of Objective-C it is acceptable to have unused parameters if the method is supposed to be overridden.
Noncompliant code example
void doSomething(int a, int b) { // Noncompliant, "b" is unused
compute(a);
}
Compliant solution
void doSomething(int a) {
compute(a);
}
Resources
- MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
- MISRA C:2012, 2.7 - There should be no unused parameters in functions
- CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
- C++ Core
Guidelines - F.9 - Unused parameters should be unnamed