当我在 C++/CLI 中从公共方法调用私有方法时,为什么会收到 CA1811?
我最近将我的项目从 Visual Studio 2008 升级到 Visual Studio 2010。
通过启用代码分析并在发布上构建,我收到警告 CA1811:避免未调用的私有代码。
我设法将代码减少为:
.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";
}
我得到的警告:
CA1811: 微软.性能: 'Foo::Bar(void)' 似乎没有 上游公共或受保护 来电者。
Bar()
是否为 static
并不重要。
我尝试用 C# 重现它,但不能。我只能用 C++/CLI 重现它。
为什么我会收到此警告?
这是 Visual Studio 2010 的错误吗?
更新
我决定打开Microsoft Connect 上的错误报告。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
微软的人重现了这个错误并决定不修复它。
解决方法是抑制警告。
非常欢迎您在 Microsoft Connect 上为该错误投票。
https://connect.microsoft.com/VisualStudio/feedback/details/560050/getting-ca1811-when-i-call-a-private-method-from-a-public- c-cli 中的方法
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
来自文档:
换句话说,我很确定微软会说这不是一个错误,只是这个规则的检测技术还没有被深入探讨。
在同一份文件中,它还说:
From the documentation:
In other words, I'm pretty sure Microsoft would say that this isn't a bug, but that the detection techniques of this rules just haven't been very deeply covered yet.
From the same documentation, it also says:
向我建议您编写了一个从未被调用的函数。
Suggests to me that you wrote a function that is never called.
如果它只发生在发布版本中,我的猜测是编译器正在放弃对
Foo::Bar
的调用,而只是让ToString()
直接返回"bar “
。您或许可以通过检查 IL 来验证这一点。If it's only happening in release builds, my guess is that the compiler is dropping the call to
Foo::Bar
and just havingToString()
directly return"bar"
. You can probably verify this by checking the IL.