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?

Gaurav Deshmukh
  • 381
  • 3
  • 15
  • I have never seen this behavior even in projects with 15k+ FxCop issues using Visual Studio analysis. Have you tried with that? – Camilo Terevinto Nov 17 '16 at 13:41
  • Are you sure the project involves async methods? The behavior is due to compile code of async methods. – Gaurav Deshmukh Nov 17 '16 at 14:12
  • Yes, it had hundreds of async methods. EDIT: I have just ran FxCop again, that behavior is not reproducible on Visual Studio analysis – Camilo Terevinto Nov 17 '16 at 14:20
  • I am using FxCopCmd – Gaurav Deshmukh Nov 17 '16 at 14:23
  • Why don't you use Visual Studio? What's stopping you? – Camilo Terevinto Nov 17 '16 at 14:26
  • I want to integrate it into CI build. – Gaurav Deshmukh Nov 17 '16 at 14:28
  • Can't you first verify that on Visual Studio works correctly? If it doesn't you will at least know it's not a FxCopCmd problem – Camilo Terevinto Nov 17 '16 at 14:30
  • I tried with this piece of code: `namespace ConsoleApplication1 { class Program { string b = ""; public async Task method1() { string a = ""; try { a.Equals("abc", StringComparison.InvariantCultureIgnoreCase); throw new NotSupportedException(); } catch (Exception) { Console.WriteLine("TP"); } return a; } static void Main(string[] args) { } } } ` – Gaurav Deshmukh Nov 17 '16 at 15:10
  • It shows same behavior – Gaurav Deshmukh Nov 17 '16 at 15:11
  • I've only spent 2 mins looking at this but hate seeing bounty questions go unanswered. Not 100% but you might give [Introspection](http://stackoverflow.com/questions/6986918/) a go. Your desire to integrate this into a CI build I'm pretty sure is not possible *yet*, see here: http://stackoverflow.com/questions/38993872/ - good question BTW – Jeremy Thompson Nov 24 '16 at 11:02

1 Answers1

2

So even after setting bounty, the question went unanswered. However, I found the workaround (if not the solution).

The mentioned problem is due to compiler generated code for async methods. Since FxCopCmd runs on dll, as compiler generated code is changed, the existing suppression messages get useless. However, Visual Studio doesn't run code analysis by merely using FxCopCmd. It runs code analysis intelligently ignoring async methods. (As per my investigation, it doesn't run any kind of code analysis on async methods. It must be due to the problem in question.)

To get the same behavior as Visual Studio in CI builds, we can use fxcoptask.dll for running FxCop analysis on code. Refer to this answer to know how to integrate FxCop in the build. This will solve the issues mentioned in the problem. Also, it gives a lot of customization options.

Community
  • 1
  • 1
Gaurav Deshmukh
  • 381
  • 3
  • 15