7

I am using FxCopCmd tool for static code analysis. Since we already had a huge codebase, we baselined existing issues using baseline.exe tool that comes with FxCop.

I am observing that if I add a new method to my C# class, then some of the suppression messages in GlobalSuppression.cs file stop working and I get issues for the code I haven't touched.

Example:

namespace ConsoleApplication1
{
    class Program
    {
        public async Task<string> method1()
        {
            string a = "";
            a.Equals("abc", StringComparison.InvariantCultureIgnoreCase);
            return a;
        }

        static void Main(string[] args)
        {

        }        
    }
}

This throws following error:

CA1031 : Microsoft.Design : Modify 'Program.d__0.MoveNext()' to catch a more specific exception than 'Exception' or rethrow the exception

To suppress the 'CA1309 UseOrdinalStringComparison' issue, I added following suppression message in GlobalSuppression.cs file

[module: SuppressMessage("Microsoft.Globalization", "CA1309:UseOrdinalStringComparison", Scope="member", Target="ConsoleApplication1.Program.d__0.MoveNext()", MessageId="System.String.Equals(System.String,System.StringComparison)", Justification="")]

But if I add one more method in the class, then this suppression message stops working. This is because method1 is async and so a new class is created (refer this) in compiled code (which was <method1>d__0 in the first case). But when I add another method before method1, then the new class created in compiled code is named <method1>d__1. Consequently, the suppression message is not applied and FxCop again starts showing errors in the code.

Is there any way to suppress FxCop errors for async methods permanently?

4

1 回答 1

2

因此,即使在设置赏金之后,这个问题也没有得到解答。但是,我找到了解决方法(如果不是解决方案)。

提到的问题是由于编译器为异步方法生成的代码。由于 FxCopCmd 在 dll 上运行,随着编译器生成的代码发生更改,现有的抑制消息变得无用。但是,Visual Studio 不会仅通过使用 FxCopCmd 来运行代码分析。它智能地运行代码分析,忽略异步方法。(根据我的调查,它不会对异步方法进行任何类型的代码分析。这一定是由于问题造成的。)

为了在 CI 构建中获得与 Visual Studio 相同的行为,我们可以使用 fxcoptask.dll 对代码运行 FxCop 分析。请参阅此答案以了解如何将 FxCop 集成到构建中。这将解决问题中提到的问题。此外,它还提供了许多自定义选项。

于 2016-11-28T17:32:41.063 回答