29

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.

takrl
  • 439

9 Answers9

14

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

Rune FS
  • 314
  • 2
  • 9
11

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."

Glorfindel
  • 3,167
ICR
  • 481
10

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".

pjc50
  • 15,223
8

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.

kevin cline
  • 33,798
5

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.

Michael Kohne
  • 10,146
3

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!

2

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"...

2

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.

2

Edit [2020/11/09]: With the advent of noexcept into the language and the deprecation and removal of the Dynamic exception specification that is referenced, this answer is out-of-date; but, it is being kept for historical reasons. Original text follows below the break.


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. 
Casey
  • 425