views:

229

answers:

3

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.

A: 

Suggests to me that you wrote a function that is never called.

DeadMG
No. There's a public ToString() that calls Bar().
brickner
A: 

If it's only happening in release builds, my guess is that the compiler is dropping the call to Foo::Bar and just having ToString() directly return "bar". You can probably verify this by checking the IL.

Mark Rushakoff
Interesting. But even if that the case, it is still a Code Analysis bug, right?
brickner
@brickner: Unlikely. `Bar` is probably still compiled into the assembly in case it needs to be accessed through reflection.
Mark Rushakoff
@Mark Rushakoff: The warning tells me to remove the unused method. If I remove it, I get a compilation warning. Doesn't that mean it's a Code Analysis bug?
brickner
A: 

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

brickner