C / C++ compiler warnings: do you clean up all your code to remove them or leave them in?

11,758

Solution 1

I would clean up any warning. Even the ones that you know are harmless (if such a thing exists) will give a bad impression of you to whoever will compile the code.

It one of the "smelly" signs I would look for if I had to work on someone else code.

If not real errors or potential future issues, it would be a sign of sloppiness

Solution 2

Clean 'em up, even if they don't indicate a real problem. Otherwise, if a warning that does indicate a real problem shows up, you won't see it through all the noise.

Solution 3

At my work, the compiler setting to treat warnings as errors is turned on. So, no warnings, or it won't compile :)

Solution 4

I agree it's best to eliminate all warnings. If you're getting thousands of warnings you should prioritize your fixes.

Start be setting your compiler to the lowest warning level. These warnings should be the most important. When those are fixed, increment your warning level and repeat until you reach the highest warning level. Then set your compile options such that warnings are treated as errors.

If you find a warning that you suspect is safe to ignore, do some research to verify your theory. Only then disable it and only in the most minimal way possible. Most compilers have #pragma directives that can disable/enable warnings for just a portion of a file. Here's 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 only disables the warning for a single line of code. Using this method also allows you to do simple text searching of your code in the future to make changes.

Alternatively you can usually disable a particular warning for a single file or for all files. IMHO this is dangerous and should only be a last resort.

Solution 5

Clean them up if possible. On a multi-platform/multi-compiler codebase (I've worked on one that compiled on 7 different OSs with 6 different compilers) that's not always possible though. I've seen cases where the compiler is just wrong (HP-UX aCC on Itanium, I'm looking at you), but that's admittedly rare. As others note, you can disable the warning in such a situation.

Many times what's a warning in this version of the compiler may become an error in the next version (anyone upgrading from gcc 3.x to 4.x should be familiar with that), so clean it up now.

Some compilers will emit really useful warnings that will become problems under certain circumstances -- Visual C++ 2005 and 2008 can warn you about 64-bit issues, which is a HUGE benefit nowadays. If you have any plans to migrate to 64-bit, just cleaning up those kinds of warnings will dramatically reduce your port time.

Share:
11,758
KPexEA
Author by

KPexEA

Video game programmer, mainly c, c++ First programmed on the Commodore Pet back in 1981 Last project Zombie Apocalypse: Never die alone Some of my memorable projects include: Test Drive (Accolade, c64) Stunts (Br0derbund, pc) Fifa International Soccer (EA, Sega Genesis) Platforms I have worked on: Commodore Pet, Vic 20, C64, Apple ][, Atari 800XL, PC, Linux, Sega Genesis, Sega CD, Sega 32x, Nintendo SNES, N64, PlayStation, PS2, PS3, Xbox, X360, STM32-Primer2, GP2X-Wiz, Chrome Native-Client

Updated on June 08, 2022

Comments

  • KPexEA
    KPexEA almost 2 years

    I've worked on many projects where I've been given code by others to update. More often than not I compile it and get about 1,000+ compiler warnings. When I see compiler warnings they make me feel dirty, so my first task is to clean up the code and remove them all. Typically I find about a dozen problems like uninitialized variables such.

    I don't understand why people leave them in and don't have perfectly clean compiles with no warnings. Am I missing something? Is there any valid reason to just leave them? Any horror stories to share?

  • Nick
    Nick over 15 years
    +1 to this post in general. The only thing I would add is that with enough "harmless" compiler warnings, it creates a lot of "noise" that can hide more dangerous warnings.
  • Len Holgate
    Len Holgate over 15 years
    I'd personally prefer to see a comment next to that #pragma warning(disable: 4312) so that I didn't have to go and look up the warning number to know what it was... If it's not obvious from the description of the warning then I'd also like a reason for why it's safe to disable it.
  • jwfearn
    jwfearn over 15 years
    good point, I actually removed the comment for brevity, I'll add it back in
  • wnoise
    wnoise over 15 years
    Right. You investigate all of them, fix the broken ones. So that any new ones aren't ignored, you have to silence the harmless ones.
  • Andrew
    Andrew over 15 years
    As it should be for any project.
  • warren
    warren over 15 years
    also - just as when fixing errors, often fixing a couple near the top fixes a slew of cascaded ones down below :)
  • Martin Beckett
    Martin Beckett over 15 years
    On windows that means you can't use MFC and would have to change all the 'C' standard lib functions to MS's s_versions
  • Nemanja Trifunovic
    Nemanja Trifunovic over 15 years
    Yes. However, I must add that for my personal projects I simply #pragma away the security warnings. _s versions are not really safe and are unportable and slow.
  • Dan
    Dan about 15 years
    Right on. I consider any warnings "visual noise" that suck away my attention from important stuff, so I kill them all when they crop up, even the ones that we all say "I know, I know, it's not a problem...."
  • dubnde
    dubnde about 15 years
    I have encountered third-party libraries or stack especially in embedded environments with lint errors I was not allowed to cleanup. The result was a interger overflow that went through to production and became a show stopped. So cleanup as much if you can and if the politics allow it.
  • Jack V.
    Jack V. over 14 years
    Yes, this. Not that long ago I didn't bother, but working on a large amount of code, it's clearly better: if you don't, new warnings are worthless. The time it most bugs me is if I'm in the middle of writing something and have a variable which isn't used yet. Of course, sometimes it's impractical: if there are already 1000s of warnings, going through and correcting them is a good thing, but it may not be able to take top priority.
  • Luis Machuca
    Luis Machuca over 10 years
    It depends on if "clean up" you mean fixing or ignoring. Depending on the compiler, you sometimes don't have any choice but to set to ignore an annoying and useless compiler warning, such as GCC's "class without a virtual destructor" one. How would the compiler know that my class is trivial and not intended for inheriting, and why should it assume the contrary? (And let's not even begin mentioning MSVC).