I am building in Visual Studio 2013, and if I add unreferenced variables to functions, the compiler does not throw warnings about them. I tried enabling code analysis, per this thread: Visual Studio 2013 Compiler Warnings not Showing but that still did not fix my issue. Some additional information:
I have the warning level set to 3 (/W3), am treating warning as errors (/WX), and am in a debug build with no optimization enabled.
My full command line from Project -> Properties -> Configuration Properties -> C/C++ -> Command Line is: /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /Fd"generated\debug\intermediate\vc120.pdb" /fp:precise /D "WIN32" /D "GLEW_STATIC" /D "_DEBUG" /D "_WINDOWS" /D "_VC80_UPGRADE=0x0710" /D "_MBCS" /errorReport:prompt /WX /Zc:forScope /RTC1 /Gd /Oy- /MTd /Fa"generated\debug\intermediate\" /EHsc /nologo /Fo"generated\debug\intermediate\" /Fp"generated\debug\intermediate\blahDebug.pch"
I'm iterating on a function that I'm constantly debugging, stepping through the code, etc. -- so I know the code is being run. But if I toss "int blah = 1;" in the function and recompile, no warnings are generated.
Example code:
bool MyClass::doSomething(int someParameter)
{
int blah = 1;
// run the normal function logic here
// 'someParameter' is referenced, but 'blah' never is.
// when i compile, i receive no warning that 'blah' is unreferenced.
return true;
}
In your example code, the statement int blah = 1;
both declares the variable and assigns to it. Visual Studio counts this assignment as a "reference" of the variable, thus avoiding the C4101 unreferenced local variable error that you are expecting.
To locate and remove variables which are initialized but never used, you can use a static analysis tool such as Prefast or CppCheck. There is a list of such tools here, though it may be out of date.
Note that the compiler can flag unused parameters, even if they are initialized with a default parameter. If you use warning level 4 via /W4
or /Wall
, then an unused parameter will cause a C4100 unreferenced parameter warning. It is a very good idea to always build at /W4
or /Wall
, rather than the default /W3
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With