当我在 C++/CLI 中从公共方法调用私有方法时,为什么会收到 CA1811?

发布于 2024-09-02 04:16:08 字数 967 浏览 2 评论 0原文

我最近将我的项目从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画中仙 2024-09-09 04:16:08

微软的人重现了这个错误并决定不修复它。

解决方法是抑制警告。

非常欢迎您在 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

魂牵梦绕锁你心扉 2024-09-09 04:16:08

来自文档

如果出现规则逻辑当前未识别的入口点,则此规则可能会报告误报。此外,编译器可能会将不可调用的代码发出到程序集中。

换句话说,我很确定微软会说这不是一个错误,只是这个规则的检测技术还没有被深入探讨。

在同一份文件中,它还说:

抑制此规则的警告是安全的。

From the documentation:

This rule can report false positives if entry points occur that are not currently identified by the rule logic. Also, a compiler may emit noncallable code into an assembly.

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:

It is safe to suppress a warning from this rule.

自由范儿 2024-09-09 04:16:08

向我建议您编写了一个从未被调用的函数。

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

可爱咩 2024-09-09 04:16:08

如果它只发生在发布版本中,我的猜测是编译器正在放弃对 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 having ToString() directly return "bar". You can probably verify this by checking the IL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文