I’ve recently upgraded my project from Visual Studio 2008 to Visual Studio 2010.
By enabling Code Analysis and building on Release, I’m getting warning CA1811: Avoid uncalled private code.
I’ve managed to reduce the code to this:
.h file:
public ref class Foo
{
public:
virtual System::String^ ToString() override;
private:
static System::String^ Bar();
};
.cpp file:
String^ Foo::ToString()
{
return Bar();
}
String^ Foo::Bar()
{
return "abc";
}
The warning I get:
CA1811 :
Microsoft.Performance :
‘Foo::Bar(void)’ appears to have no
upstream public or protected
callers.
It doesn’t matter if Bar() is static or not.
I’ve tried to reproduce it in C# but I can’t. I can only reproduce it in C++/CLI.
Why do I get this warning?
Is this a Visual Studio 2010 bug?
UPDATE
I’ve decided to open a bug report on Microsoft Connect.
Microsoft guys have reproduced this bug and decided not to fix it.
Suppressing the warning is the workaround.
You are more than welcome to vote for this bug at Microsoft Connect.
https://connect.microsoft.com/VisualStudio/feedback/details/560050/getting-ca1811-when-i-call-a-private-method-from-a-public-method-in-c-cli