Disabling compiler warnings

Back to Programming techniques

When a compiler emits a warning, it might or might not mean that changes are warranted in the code itself. While most of the warnings are appropriate, some are not, and further, to some warnings there are no practical solutions. In these cases, instead of modifying your code, disabling the warnings is the right thing to do. Here we list all the warnings we have disabled in Pastel, and explain why we have done that.

Visual Studio

4018 ‘expression’ : signed/unsigned mismatch
This warning notifies that a signed-unsigned integer conversion is taking place. This conversion is generally an information-destroying process in both directions. Generally, you should avoid this situation by not using unsigned integers. However, if this not possible, there is nothing you can do. While a cast might get rid of the warning, it does not get rid of the problem. A better approach is to disable the warning: if at some later time an unsigned-signed conversion is thought to cause problems, then, by enabling the warning, the compiler can help you to locate those places again.
4146 unary minus operator applied to unsigned type, result still unsigned
4180 qualifier applied to function type has no meaning; ignored
4224 nonstandard extension used : formal parameter ‘identifier’ was previously defined as a type
4244 ‘argument’ : conversion from ‘type1’ to ‘type2’, possible loss of data.

For int a = 1.0f;, the warning refers to a floating point type being converted to an integer type. It is self-evident that there is possible loss of data when doing the conversion, and this is most of the time also the intention (rounding). This warning essentially urges to use a cast to make the conversion explicit to the reader. From the compilers view, this cast is redundant, since the implicit conversion will take effect in any case, and can be seen as a case-by-case supression of the warning. We find the redundant casts making the code less clear, and thus disable the warning instead. At a later time, if desired, the compiler can help to locate these places by enabling the warning.

For long long c = 1; int b = c;, the warning is more to the point. However, since we use the integer type exclusively to store amounts, this is not a problem. See the explanation for the warning 4267 below to see why this is.

4251 ‘identifier’ : class ‘type’ needs to have dll-interface to be used by clients of class ‘type2’
4267 ‘var’ : conversion from ‘size_t’ to ‘type’, possible loss of data
4305 ‘identifier’ : truncation from ‘type1’ to ‘type2’
4312 “‘operation’ : conversion from ‘type1’ to ‘type2’ of greater size”
4351 new behavior: elements of array ‘array’ will be default initialized
This warning notifies of changed behaviour compared to a previous version of Visual Studio. Because the new behaviour is the correct one as defined by the C++ standard, the warning carries no information.
4584 ‘type’ : base-class ‘type2’ is already a base-class of ‘type3’
4800 ‘type’ : forcing value to bool ‘true’ or ‘false’ (performance warning)
4804 ‘operation’ : unsafe use of type ‘bool’ in operation
4996 ‘function’: was declared deprecated
This warning is seen on common C++ Standard Library functions, and is an incorrect one. This warning appeared alongside with the Microsoft’s ‘safe’implementation of the STL in Visual Studio 2008. Considering that the reported C++ Standard Library functions are not deprecated in the C++ Standard, and will not be deprecated in the next C++ standard either, the only motivation seems to be to promote the safe implementation.

g++ and Clang

-Wno-unknown-pragmas
Pragma warnings caused by OpenMP support not being enabled.
-Wno-sign-compare
Comparison between an unsigned and a signed integer.
-Wno-sign-conversion
Conversion between an unsigned and a signed integer.
-Wno-unused-variable
Unused variables.
-Wno-unused-value
Unused values.
-Wno-unused-function
Unused functions.
-Wno-strict-aliasing
Breaking strict aliasing rules.
-Wno-strict-overflow
Compiler warns that it optimizes code based on the assumption that signed integer overflows do not occur.

g++

-Wno-unused-but-set-variable
Unused but set variable.
-Wno-unused-local-typedefs
Unused local typedef

Clang

-Wno-tautological-compare
Compiler warns that >= 0 is always true for an unsigned integer.