Search for unused objects (non-primitive values)

Follow the question: g ++ does not show a 'unused' warning .

I fully understand why g ++ does not warn about these variables, but I would like it to find them somehow. The code I'm working on does not have any special cases, so one FloatArray x; almost definitely left.

Even if I need to mark separate classes (for example, a warning for unused FloatArray objects), this would be very useful. What can I do?

+6
source share
3 answers

Well, with GCC, the following code warns you how you want:

 struct Foo { }; struct Bar { Foo f; }; int main() { Bar b; //warning: unused variable 'b' } 

But if you add a constructor / destructor to the Foo or Bar structure, even trivial, it will not warn.

 struct Foo { Foo() {} }; struct Bar { Foo f; }; int main() { Bar b; //no warning! It calls Foo::Foo() into bf } 

Thus, the easiest way to return a warning is to compile all the relevant AND destructors constructors conditionally:

 struct Foo { #ifndef TEST_UNUSED Foo() {} #endif }; struct Bar { Foo f; }; int main() { Bar b; //warning! } 

Now compile with g++ -DTEST_UNUSED to check for additional unused variables.

Not my brightest idea, but it works.

+3
source

Well, basically you want to create some kind of simple static analysis tool connected to GCC? If so, you can start by using MELT to quickly implement an unused variable printer.

http://gcc.gnu.org/wiki/MELT%20tutorial

+1
source

I'm not sure if I missed something in the question, but gcc / g ++ has options that allow you to specify which warnings you want and which not. So just simply included -Wunused-variable.

See here http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html for more details.

In addition, -Wall will include this and many more useful warnings.

-1
source

Source: https://habr.com/ru/post/906341/


All Articles