Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-Wextra how useful is it really?

Tags:

gcc

I'm reading the gcc manual at the moment, especially the part about warning/error flags. After reading the part about the -Wextra flag, I wonder if it is useful at all. It seems that it complains about things which seem to be rather subjective or a matter of taste. I'm not that experienced with gcc, I only use it from time to time for some small projects at university, so to all experienced C/C++ (or for whatever language you use gcc), what's the deal with -Wextra?

like image 236
helpermethod Avatar asked May 22 '10 14:05

helpermethod


2 Answers

-Wextra, among other stuff implies -Wtype-limits:

Warn if a comparison is always true or always false due to the limited range of the data type, but do not warn for constant expressions. For example, warn if an unsigned variable is compared against zero with '<' or '>='. This warning is also enabled by -Wextra.

I find this really useful.

like image 163
nc3b Avatar answered Nov 11 '22 20:11

nc3b


I usually add -Wno-sign-compare and -Wno-unused-parameter to remove noise.

The implied -Wuninitialized (with the -O2 option) has been very helpful to me, but adding it initially to your code base can be a bit daunting. One way to deal with this is to add two macros (which look slightly strange: the equal sign is intentional):

#define ELIMINATE_GCC_WARNING = 0 // used to remove nuisance warnings
#define UNCHECKED_GCC_WARNING = 0 // not yet verified

Then you can quickly eliminate the gcc warnings and get a clean compile by using e.g.:

int foo UNCHECKED_GCC_WARNING;

Then as an optional step go back and check these additions, and change them one-by-one to ELIMINATE_GCC_WARNING. This can be slow. But I would be surprised if you didn't find some existing bugs.

like image 23
Joseph Quinsey Avatar answered Nov 11 '22 20:11

Joseph Quinsey