c++variablescastingvoidunused-variables

Auto cast to void unused variable C++


I am trying to solve huge number of warnings in a C++ project that is generated by a lot of unused variables. For example, consider this function:

void functionOne(int a, int b)
{
    // other stuff and implementations :D
    doSomethingElse();
    runProcedureA();
}

In my project, in order to surpress the warnings I am simply casting the unused variables to void because I cannot change the methods signatures.

void functionOne(int a, int b)
{
    (void)a;
    (void)b;
    // other stuff and implementations :D
    doSomethingElse();
    runProcedureA();
}

This technique works all right but I have a really huge quantity of functions that I need to do this in order to solve the warnings issue. Is there any way to auto refactor all these functions by casting all unused parameters to void?

Currently, I am working with CLion IDE and VSCODE.


Solution

  • A simple alternative is to not give names for the parameters instead of the cast. This way the unusage would be considered intentional:

    void functionOne(int, int)
    

    Another way to achieve the same is:

    void functionOne([[maybe_unused]] int a, [[maybe_unused]] int b)
    

    Is there any way to auto refactor all these functions

    Potential XY-problem: If you don't want to be warned about unused parameters, how about disabling the warning?