Home Dashboard Directory Help
Search

Getting CA1811 when I call a private method from a public method in C++/CLI by BricknerB


Status: 

Closed
 as Won't Fix Help for as Won't Fix


2
0
Sign in
to vote
Type: Bug
ID: 560050
Opened: 5/19/2010 4:31:18 AM
Access Restriction: Public
0
Workaround(s)
view
1
User(s) can reproduce this bug

Description

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.

I believe this is a Visual Studio 2010 bug since I call the private method from the public method and I can't remove the private method without breaking compilation.
Details
Sign in to post a comment.
Posted by BricknerB on 6/10/2010 at 10:05 PM
If I suppress it, removing the calls to the private code will not result with a warning.

Is there a planned fix for this problem?

Thank you,

Boaz.
Posted by Microsoft on 6/10/2010 at 3:31 PM
Hi,

Thank you for your feedback. The reason why you get the CA1811 warning is because the compiler has optimize the code such that the method body of Bar() has been inlined into ToString() where Bar() is called. After it's compiled, basically the call looks as if it was typed as this:

String^ Foo::ToString()
{
return "abc";
}

That's why it sees Bar not being called. You can workaround this issue by adding a suppression for this warning.

thanks,
Brahmnes
Posted by Microsoft on 5/19/2010 at 5:02 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.