0

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.

fborges22
  • 313
  • 3
  • 14
  • 2
    At that point you may as well silence the warning instead, in my opinion. The warning is trying to point out possible mistakes, but if you automate the process of fixing them you would miss any actually useful warnings anyway, so it serves no purpose. – François Andrieux May 13 '21 at 18:49
  • 2
    Can you silence this warning via a compiler switch, see: https://stackoverflow.com/a/19431868/5743288? – Paul Sanders May 13 '21 at 18:50
  • If you are looking for a refactoring tool - I suggest to look for something that would remove those unneeded parameters from function declaration/definition. I could ask - why are there unused parameters, but I guess I know the answer: `legacy API`... – Vlad Feinstein May 13 '21 at 19:08
  • Yes it is indeed legacy API... I am modernizing the code and keep compatibility – fborges22 May 13 '21 at 19:10
  • 4
    `void functionOne(int , int )` is another way of getting rid of warning. This does not change function signature. – SergeyA May 13 '21 at 19:11
  • Interesting suggestion... I will try this one – fborges22 May 13 '21 at 19:17
  • 1
    Quick search through Visual Studio Code Marketplace resulted in ONE (!) extension to refactor C++ code, and it doesn't offer that: https://marketplace.visualstudio.com/items?itemName=tdennis4496.cmantic. How many warnings are there? I Will it be worth your time to create a parser for your compiler output to auto-fix them? – Vlad Feinstein May 13 '21 at 19:20

1 Answers1

5

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?

eerorika
  • 232,697
  • 12
  • 197
  • 326