Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
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
add comment

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

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

7 Answers

up vote 5 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 warings. However while devloping a 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 ment 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
add comment

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
add comment

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
2  
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
show 2 more comments

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
add comment

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
add comment

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
add comment

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
add comment

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.