Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This answer and the comments added to it show a way to disable several compiler warnings using #pragma directives.

Why would one want to do that? Usually the warnings are there for a reason, and I've always felt that they're good reasons. Is there any "valid case" where warnings should be disabled? Right now I can't think of any, but maybe that's just me.

share|improve this question

migrated from stackoverflow.com Jun 24 '11 at 12:05

This question came from our site for professional and enthusiast programmers.

3  
Not sure why someone marked this for close. Seems like an eminently reasonable question to me. +1 – Alastair Pitts Jun 24 '11 at 7:01
    
@Alastair Pitts: I proposed a migration to Programmers. I realized my mistake later. – junjanes Jun 24 '11 at 7:22
3  
The warning messages are there for a reason, yes, but there's also a reason why they aren't error messages. – james large yesterday
2  
@jameslarge Your comment sums up the situation nicely. A warning is the compiler telling you that a situation is plausibly wrong, which implies possibly right. If it was definitely wrong then it would be an error. Since some warnings may be false positives, there should always be a way to write code such that it eliminates the warning. Unfortunately, sometimes the most pragmatic way to do so is via a pragma; hence the name. – Eric Lippert yesterday
up vote 9 down vote accepted

I've only ever had one situation where I disabled a warning. I consider warnings errors so I wouldn't normally release with warnings. However, while developing an API at a customers I faced the issue that a method that was needed in a migration phase by one application and that no other should ever use had to be included in the library.

The best way I could find to tell all users of the API that they shouldn't call this method was to mark it obsolete. That, however, meant that the one valid use case was marked as a compile warning.

Eric Lippert has written a few posts about warnings where you'll find information about how the compiler team thinks about warnings.

Internal fields of Internal types

Unused using directives are not marked with warnings

share|improve this answer

Here are a few warnings where the documentation gives reasons why you might want to disable them:

Other examples include warnings about using depreciated methods if you know you still want to use the old method or having private members that are never read locally, but with reflection instead.

In my experience C# has less need for disabling warnings than other languages such as C++. This is largely because, as Eric Lippert says in his blog, they "try to reserve warnings for only those situations where we can say with almost certainty that the code is broken, misleading or useless."

share|improve this answer
3  
Nice. I think the first one is clearest, because it provides a very specific case and a justificaiton (the third, for example, shows a peice of code that would never pass review on my team). This question talks about obsolete/depricated warnings. Basically, they are still needed in legacy code, but you want to discourage any new code from using them. The legacy code should have the warnings suppressed. – Greg Jackson Jun 24 '11 at 7:23
    
I've done a lot of macro programming in excel and I had to disable warnings for various reasons such as auto save, auto exit, notifications, etc. Of course you may not be on about these warnings... – Dave Mess Jun 24 '11 at 15:10
    
@ICR I don't remember disabling compiler warnings in Java at all. All I do is avoid using deprecated methods. – Mahmoud Hossam Jun 26 '11 at 9:55
    
@Mahmoud I very often find myself having to surpress "unchecked" warnings when doing anything even remotely complex with generics. But it's probably unfair to lump Java in with C++ on the ridiculous warnings front - edited my answer. – ICR Jun 26 '11 at 16:54
    
@ICR Java enforces the use of generics to provide type safety in collections, while some view this as a constraint, I think of it as a feature, it makes writing code a bit painful, but it saves lives, and yeah, C++ compiler output is somewhat scary when it comes to STL or anything with templates. – Mahmoud Hossam Jun 26 '11 at 17:01

Many indispensible Java libraries have never been updated to eliminate the need for unsafe typecasts. Suppressing those warnings is necessary so that other more important warnings will be noticed and corrected.

share|improve this answer

I do embedded work and I seem to remember a time or two when I've disabled warnings because I was doing something that looked useless to the compiler, but which actually had real-world effects in the hardware.

The only other time is when I'm working on codebases with disparate ideas of some data structure (like how to represent arrays of bytes - char or unsigned char?). In these cases I might disable the warnings because the alternative is to spend days going through the code and either modifying one portion, or putting in hundreds of casts.

share|improve this answer

An example in C that I encounter variants of regularly:

int doSomething(int argument1)
{
#ifdef HARDWARE_TYPE_A
    performAction(argument1);
#else
    displayNotSupportedMessage();
#endif
}

The argument is only relevant on some platforms, but on the ones where it's not relevant my compiler will complain, and since I have warnings converted to errors it will prevent it building.

Converting warnings to errors pretty much requires an escape hatch for "not this one, I know better than the compiler in this case".

share|improve this answer

Valid or not, it is sometimes done to bypass the "treat warnings as errors" directive on the build server.

Other than that I can't think of any either. Disabled warnings are usually a sign of an "ugly hax"...

share|improve this answer

Last time we disabled certain warnings, it was because an intern had left us with bad code. I've got it working a lot better, with clear conversion boundaries replacing the haphazard data representation.

In the meantime, we needed to compile it in, and we wanted the "warnings are errors" option on, so we suppressed some of the warnings.

share|improve this answer

Currently, the only warning I ever ignore is

  warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)  

Because microsoft does not implement the C++ specification (the documentation even says they don't!) and allow functions to declare specific throws and all functions can only throw either throw() or throw(...), i.e nothing or everything.

From HelpViewer 1.1:

 A function is declared using exception specification, which Visual C++ accepts but does not implement. Code with exception specifications that are ignored during compilation may need to be recompiled and linked to be reused in future versions supporting exception specifications. 
share|improve this answer

There are quite a few reasons to selectively disable compiler warnings, even for projects which strive for best practices.

  • Different Compilers (or different versions of the same compilers):
    Compilers handle warnings in subtlety different ways. Giving false-positive warnings that don't impact other compilers. In this case it might make sense to disable the warning for those compilers, instead of editing valid code to quiet a false-positive warning that only impacts certain compilers, especially for older compilers which will eventually become unsupported anyway.
  • With generated code:
    Some warnings related to code hygiene (dead code, duplicate body of conditional statements, comparisons that exceed type limits) can be safely ignored since they are harmless and the compiler will optimize them out.
    Generating code that doesn't raise these harmless warnings is of course an option too, but may be more trouble then its worth.
  • Warnings for external code:
    Perhaps you use a well known qsort or md5 checksum implementation which is included in your project. The code is used by many projects and known to work well, yet there may be some picky warnings which your normally correct for your own code.
    For external code however, it may be less hassle to just disable the warning (assuming its definitely is harmless).
  • Warnings caused by system headers:
    Even though GCC/Clang for example support -isystem, there are cases when differences in system headers cause warnings that can be ignored (perhaps a function has a signed return value on one system but not another), triggering -Wsign-compare warnings.
    Another case may be macros defined in system headers, you could copy-paste the macros into your own code to modify them, but all things considered its better not to have to worry about maintaining macros from 3rd party libraries... so its better just to quiet the warning (perhaps the macro misses a cast causing -Wsign-conversion eg).
  • Unused warnings in stub code:
    You may warn of unused parameters, however when stubbing out an entire library in a single file that only contains stub functions - its not helpful to force (void)arg1; (void)arg2; (void)arg3; ... in the body of every stub function.
    Better just suppress -Wunused-parameter in this case.

Note that in all these examples, its assumed disabling the warnings is not going to hide real bugs, eg: -Wredundant-decls, -Wunused-parameter, -Wdouble-promotion, perhaps -Wpedantic... and that you know what you're doing!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.