Improvements to Warnings in the C++ Compiler
We made a number of improvements to C++ compiler warnings in Visual Studio 2015 Preview.
The printf family of functions
The community has made it clear that printf and its cousins are still heavily used and would benefit from more rigorous diagnostics when used. In Visual Studio 2015 Preview, the compiler will check that the number of arguments you provide to these functions match the number of arguments expected by the format string:
printf(“%d %dn”, 1); //C4317
printf(“%dn”, 1, 2); //C4422
The compiler will emit:
warning C4317: ‘printf’ : not enough arguments passed for format string
warning C4422: ‘printf’ : too many arguments passed for format string
Furthermore, specifiers that aren’t allowed to be used in some variants of printf will also emit warnings:
printf_s(“Test %n”, &n); //C4426
Here, printf_s is the security-enhanced version of printf, which disallows %n. The warning is:
warning C4426: ‘%n’ is not allowed in the format string of function ‘printf_s’
In Visual Studio 2015 RTW, we will add warnings that also check the types of the parameters passed to these functions.
A variable declaration “shadows” another if the enclosing scope already contains a variable with the same name. For example:
void f(int x)
char x; //C4457
char y; //C4456
The inner declaration of x shadows the parameter of function f, so the compiler will emit:
warning C4457: declaration of ‘x’ hides function parameter
The inner declaration of y shadows the declaration of y in the function scope, so the compiler will emit:
warning C4456: declaration of ‘y’ hides previous local declaration
Note that, as before, a variable declaration with the same name as a function parameter but not enclosed in an inner scope triggers an error instead:
void f(int x)
char x; //C2082
The compiler emits:
error C2082: redefinition of formal parameter ‘x’
If you use /WX, these new warnings could cause some of your old projects to no longer build. To work around this, you can use #pragma warning or the /wd compiler option to disable them. Alternatively, a new compiler option has been added to allow you to control the set of warnings emitted by the compiler, in order to facilitate the compilation of code that you don’t wish to modify. To suppress warnings introduced after compiler version XX.YY.ZZZZ, use /Wv:XX.YY.ZZZZ. XX is the major version number of the compiler, which is 15 for Visual Studio 2008, 16 for Visual Studio 2010, 17 for Visual Studio 2012, and 18 for Visual Studio 2013.
However, for projects that are still in active development, we strongly encourage you to keep all new warnings enabled and fix the code instead, because they will likely help you write programs that are more robust and more secure.