How to get g ++ to warn about unused member variables

g ++ generates warnings for unused local variables. Is it possible to have a g ++ warning for unused class member variables and / or global variables?

class Obj { public: Obj(int a, int b) : num1(a), num2(b) {} int addA(int i) { return i + num1; } private: int num1; int num2; }; 

How do I get g ++ to warn me that num2 not used?

UPDATE: I am currently compiling with:

 g++ -Wall -Wextra -pedantic *.cc -o myprogram 
+6
source share
3 answers

I do not know about such a warning. In addition, I will assume that the reason why it does not exist is that it cannot be reliably formed in all cases, so they decided not to spend efforts to make it work for some subsets of cases. For example, if the friend class is another function that is in the library, the compiler might not know if this library has changed any particular attribute of the class or not.

+2
source

You can use cppcheck ( download ), cppcheck --enable=style does exactly what you need, among other useful things.

+3
source

Clang -Wunused-private-field resolves the warning you are asking for. Based on your code base, it shows:

 $ clang -Wunused-private-field /tmp/nic.cpp /tmp/nic.cpp:10:22: warning: private field 'num2' is not used [-Wunused-private-field] int num2; ^ 1 warning generated. 
+1
source

All Articles