Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it best practice to try to remove ALL compiler warnings with any means [closed]

our project manager recently set up a policy to request developers to remove ALL the compiler warnings. I understand some warnings really are easy to be removed, some are not so easier. So some developers uses every possible way to achieve the goal. For example, using explicit cast to cast double to float, float to int, signed to unsigned etc, etc. Since our code base is so large, over 20 years of 30-50 developers' work, I really doubt that how much this effort can really help us, if it does have some merits. Can anyone give some of your advice or arguments? our project uses C++.

like image 258
nowgains nowpains Avatar asked Jan 25 '13 15:01

nowgains nowpains


People also ask

Do compiler warnings matter?

They are not errors from the viewpoint of a programming language, but they may be software bugs. However, many compilers can be customized so that their warnings don't stop the compilation process. Warnings must not be ignored. You'd better fix every possible error before starting software testing.

Why is it a good idea to always enable compiler warnings in C?

Not only does handling the warnings make better code, it makes you a better programmer. Warnings will tell you about things that may seem little to you today, but one day that bad habit will come back and bite your head off. Use the correct type, return that value, evaluate that return value.

Should I treat warnings as errors?

Yes, even once in a while you will encounter the occasional warning you'd be better off leaving as a warning or even disabling completely. Those should be the exception to the rule, though. Here's some practical advise: at the start of a new project, start treating all warnings as errors, by default.

How do you ignore a warning when compiling?

Using legacy __attribute__ to provide more information to the compiler. Using _Pragma to suppress the warning. Using #pragma to suppress the warning. Using command line options to suppress the warning.


2 Answers

Once you let compiler warnings slip, the 'real' warnings also get ignored as well. I can't count the number of times I've come onto a project with a ton of warnings, that with just a little care, were removed along with a bunch of very subtle bugs. Accidental assignment in an if, accidental fall-through in a switch or no default, accidental ; at the end of a for loop, unintentional type-cast etc. The warnings are there for a reason - use them. Yes, some are very pendantic, but just a little bit of work saves a lot of headache later on.

Clean the warnings and keep them clean. You will write better code.

like image 73
Michael Dorgan Avatar answered Oct 04 '22 04:10

Michael Dorgan


It can be quite tricky to fix all warnings in a large project. And some compilers have rather obscure sense of "right things to warn for".

It is definitely a good thing to fix warnings. But it should be done the right way - and sometimes, the correct thing is to simply disable that warning.

Some really annoying ones are "unused parameters" - well, if the interface determines that this function is called with two parameters, but you only use one (or none) because for what this particular implementation does isn't using those parameters, then it's hard to fix the warning [you can of course remove the name of the varivable (or put it as a comment)].

Similarly, sometimes compilers can be so picky that it's impossible to be right - I've had cases where not having a return at the bottom of the function gives "not all paths lead to a return" and when I add a return at the bottom of the function, it says "unreachable code" - well, how do you want it? This is where (for that section of code) disabling the relevant warning is the right thing to do.

It gets worse if you have to compile the code with multiple compilers - sometimes what is "good" in compiler, turns into something that is a warning in another, and the fix for that warning turns into a warning in the next compiler, which can be tricky to solve.

Once you have a strategy for coping with warnings, and have removed the ones that you want to remove, disabled the ones you want to disable, I would suggest building your product with "tread warnings as errors".

Most of the time, warnings are "good" for the programmer, and should not be ignored. But there are times when "I know what I'm doing, shut up compiler!" is the right approach - of course, most of the time, those are fixed by applying a cast or some such - and that should be done when it's the right way to fix the problem.

like image 20
Mats Petersson Avatar answered Oct 04 '22 05:10

Mats Petersson