Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the consequences of ignoring: warning: unused parameter

Tags:

I am working on a C++ project and I noticed that we have a number of warnings about unused parameters.

What effect could it have if these warnings are ignored?

like image 599
Phil Hannent Avatar asked Nov 21 '08 09:11

Phil Hannent


People also ask

What is unused parameter?

Code Inspection: Unused parameterThe parameter is passed by value, and the value is not used anywhere or is overwritten immediately. The parameter is passed by reference, and the reference is not used anywhere or is overwritten immediately.

What is unused parameter in C?

In GCC, you can label the parameter with the unused attribute. This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

Why is unused variable an error?

They are warnings, if they were errors they had the bad effect of failing the compilation. Unused variable warnings are emitted during compiletime and should be a hint to you as the developer, that you might have forgotten to actually process a value that you have bound to a name. Thats the main reason for the warning.


1 Answers

The function with an unused parameter may have a real bug in the following cases:

  1. There is an output parameter, which is not being assigned or written into, resulting in undefined value for the caller.

  2. One of parameters is a callback function pointer, which you must invoke and forget to do so. May happen if there is a lot of #ifdefs in the function.

  3. You declare a local variable with the same name that shadows a parameter and subsequently use the wrong value down in the function.

Not using an input parameters may be harmless, but you can reduce the noise to see useful warnings by marking unused input parameters explicitly in the beginning of the function by casting it to void (works for both C and C++):

(void)param1; 

Or,

#define UNUSED(expr) do { (void)(expr); } while (0) ...  void foo(int param1, int param2) {     UNUSED(param2);     bar(param1); } 

Or omit parameter name (C++ only):

void foo(int param1, int /*param2*/) {     bar(param1); } 
like image 57
Alex B Avatar answered Oct 31 '22 04:10

Alex B