【问题标题】:False positive for CA1801 in Release config only仅发布配置中 CA1801 的误报
【发布时间】:2013-04-20 10:18:01
【问题描述】:

我的方法如下。

public static void Debug(this Logger logger, string message)
{
    logger.Debug(() => message);
}

当我使用 Debug config 运行代码分析时,我没有收到任何警告。

当我使用发布配置运行代码分析时,我得到:

CA1801 查看未使用的参数“CommonExtensions.Debug(this Logger, string)”的参数“message”从未使用过。删除参数或在方法体中使用它。 FakeItEasy CommonExtensions.cs 101

CA1801 查看未使用的参数“CommonExtensions.Debug(this Logger, string)”的参数“logger”从未使用过。删除参数或在方法体中使用它。 FakeItEasy CommonExtensions.cs 101

这显然是错误的。我在方法体中使用了这两个参数。如果我删除任何一个参数,则代码不会编译。

有没有其他人经历过类似的事情?我是否发现了代码分析中的错误?

(我在两种配置下使用相同的规则集。)


更新

Logger.Debug()签名如下:

[Conditional("DEBUG")]
public abstract void Debug(Func<string> message);

【问题讨论】:

  • Logger.Debug() 的声明是什么?
  • 我用public void Debug(Func&lt;string&gt; f) 试过这个,对我来说效果很好(所有CA警告都打开了)。这适用于 VS2012 Update 1、.Net 4 或 .Net 4.5 版本。
  • @MatthewWatson 谢谢 - 你提示我找到答案。很快就会更新

标签: c# code-analysis


【解决方案1】:

根据 Q 更新,Logger.Debug() 设置了 Conditional 属性,这就是原因。

【讨论】:

    【解决方案2】:

    您可以通过添加条件"CODE_ANALYSIS" 属性来修复它:

    [Conditional("DEBUG")]
    [Conditional("CODE_ANALYSIS")]
    public abstract void Debug(Func<string> message);
    

    (我有same problem,发现a solution。)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多