C / C ++ Compiler Warnings: Are you clearing all your code to delete them or leave them?

I worked on many projects where they gave me code for other users to update. Most often, I compile it and get about 1000+ compiler warnings. When I see compiler warnings, they make me feel dirty, so my first task is to clear the code and delete them all. Usually I find about a dozen problems, such as uninitialized variables.

I do not understand why people leave them and do not have absolutely clean compilations without warnings. Am I missing something? Is there a good reason to just leave them? Any horror stories to share?

+59
c ++ c compiler-construction warnings
08 Oct '08 at 17:01
source share
18 answers

I would clear any warning. Even those that you know are harmless (if such a thing exists) will be bad for you who compile the code.

This is one of the "smelly" characters that I would look for if I had to work on another's code.

If not real mistakes or potential future problems, this will be a sign of sloppiness.

+82
Oct 08 '08 at 17:05
source share

Clean them even if they do not indicate a real problem. Otherwise, if a warning about what indicates indicates that a real problem is occurring, you will not see it in all the noise.

+46
Oct 08 '08 at 17:09
source share

In my work, a compiler option is included to handle warnings as errors. So, no warnings, or it will not compile :)

+38
Oct 08 '08 at 17:06
source share

I agree that it is best to clear all warnings. If you get thousands of alerts, you must prioritize your fixes.

Run your compiler to the lowest warning level. These warnings should be the most important. When they are fixed, increase the warning level and repeat until you reach the highest warning level. Then set the compilation options so that warnings are treated as errors.

If you find a warning that you suspect is safe to ignore, do some research to test your theory. Only then turn it off and only in the most minimal way. Most compilers have #pragma that can disable / enable warnings for only part of the file. Here is a Visual C ++ example:

 typedef struct _X * X; // from external header, not 64-bit portable #pragma warning( push ) #pragma warning( disable: 4312 ) // 64-bit portability warning X x = reinterpret_cast< X >( 0xDDDDDDDD ); // we know X not 64-bit portable #pragma warning( pop ) 

Note that this disables the warning for only one line of code. Using this method also allows you to perform a simple text search of your code in the future to make changes.

Alternatively, you can usually turn off a specific warning for a single file or for all files. IMHO this is dangerous and should be only last.

+19
Oct 08 '08 at 17:25
source share

Clean them if possible. . On a multi-platform / multi-compiling code base (I worked on one that was compiled on 7 different OS with 6 different compilers), which is not always possible. I have seen cases where the compiler is simply wrong (HP-UX aCC on Itanium, I look at you), but this is admittedly rare. As others have noted, you can turn off the warning in this situation.

It is many times that a warning in this version of the compiler may become a bug in the next version (anyone who upgrades from gcc 3.x to 4.x should be familiar with this), so clear it now.

Some compilers will generate really useful warnings that will become specific problems in certain circumstances. Visual C ++ 2005 and 2008 can warn you about 64-bit issues, which are currently a huge advantage. If you plan to upgrade to 64-bit, just clearing these warnings will significantly reduce your port time.

+14
Oct 08 '08 at 17:41
source share

There are several cases where I leave warnings in the code or where it is impossible to clear them (although I delete those that I can). For example:

  • If you have something you're working on and you know that he needs more work / attention, leaving a warning to indicate that this might be appropriate.
  • If you compile C ++ with / clr, there are a few warnings about things that cause you to create your own code; this can be cumbersome to suppress all of these warnings when the functional code variability cannot be functionally changed.
  • Clear alerts when you don’t understand what the fix is ​​doing. I did this a couple of times with a PC-Lint warning and ended up typing errors. If you do not know what the exact effect of the change is (for example: C-style to exclude warnings), DO NOT. Sorry about the warning or leave the code alone, this is my advice.

In any case, these are the cases that lie on my head where warnings may be appropriate.

+9
Oct 08 '08 at 18:37
source share

The worst part is that when you write new code, it is difficult to recognize it if you accidentally presented more warnings, as there are so many just ignored them.

The problem with cleaning them is that for this you will need time, or maybe not. But yes, as a rule, you should clean as much as you can.

+6
Oct 08 '08 at 17:08
source share

Providing warnings in code because you don’t have time to fix them, it’s not to brush your teeth because you don’t have enough time in the morning. This is the main issue of code hygiene.

+6
Oct 08 '08 at 19:18
source share

Always clear your warnings. If you have a specific case where you know that a warning is in order, then suppress it only for that instance.

Although some warnings may be benign, most of them indicate a real code problem.

If you do not clear all of your warnings, the list of warnings will continue to grow, and real problems will be lost in a sea of ​​warning noise.

+5
Oct 08 '08 at 17:07
source share

One of the characteristics of a really good programmer is that bad code gives them a sick stomach.

I try to ensure that all my code is not only a clean compiler, but also is clean in my IDE to a fairly legible level. Sometimes I need to suppress a warning instance if I know better than a tool, but at least that also serves as documentation.

+4
Oct 08 '08 at 17:08
source share

I always turn on all warnings and then set my project to stop if there are any warnings.

If there are warnings, you need to check each one to make sure there are no problems. Doing it again and again is a waste of time. Without doing this, it will cause errors in your code.

There are ways to remove the warning (e.g. #pragma argsused).

Let the compiler do the work.

+3
Oct 08 '08 at 20:28
source share

I have worked with a number of embedded systems where warnings lead to instability, crashes, or memory corruption. If you do not know that a warning is harmless, it should be addressed.

+2
08 Oct '08 at 19:16
source share

Warnings are and should be treated as errors. If you cannot code correctly to get rid of your warnings, you probably shouldn't code. In my group, we decided to send all warnings to errors. He completely ends this discussion and really, IMHO, improves the quality of the code.

+1
Oct 08 '08 at 17:16
source share

I do not like warnings. I delete them as many as possible.
Sometimes, when I am under pressure to finish work, I leave some of them. I rarely leave them. I feel like you, dirty, if there are any leftists.

0
Oct 08 '08 at 17:06
source share

The code database contains more than 4000 warnings. Some of these are legitimate issues. We never take the time to enter and fix them, and also not to reorganize other broken things ... This is partly due to the fact that the code is so old that it precedes standardized C ++. We can only compile in VC ++ 6.

0
Oct 08 '08 at 17:07
source share

Always clear all warnings or explicitly suppress them, if necessary. The default settings for warnings should be as high as possible during compilation (for example, level 4 on VS).

0
Oct 08 '08 at 17:18
source share

My boss who created some code that I currently support. It uses compiler flags to hide its deprivation warnings.

When I have time, I look through and clean what I can.

0
Oct 08 '08 at 23:02
source share

I am trying to compile code with a fairly high level of warnings and clear them all, with the exception of “signing / unsigned comparison” warnings, which I am sure I have to fix, but cannot be bothered.

Short version: In g ++, I use "-Wextra -Wno-sign-compare" and get rid of all messages.

0
Oct 11 '08 at 16:28
source share



All Articles