FxCop: suppression message for async method

I use the FxCopCmd tool for static code analysis. Since we already had a huge code base, we based our existing problems using the baseline.exe tool that comes with FxCop.

I observe that if I add a new method to my C # class, some of the suppression files in the GlobalSuppression.cs file will stop working, and I get problems for the code that I did not touch.

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 causes the following error:

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

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

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

But if I add another method to the class, this message will stop working. This is because method1 is asynchronous, and so a new class is created ( referenced by this ) in the compiled code (which was <method1>d__0 in the first case), but when I add another method before method1, a new class created in The compiled code is called <method1>d__1 . Therefore, the suppression message is not applied, and FxCop again starts to show errors in the code.

Is there a way to suppress FxCop errors for asynchronous methods permanently?

+6
source share
1 answer

Thus, even after setting up generosity, the question remained unanswered. However, I found a workaround (if not a solution).

The indicated problem is related to the code generated by the compiler for asynchronous methods. Because FxCopCmd works in the dll, as the generated compiler code changes, existing suppression messages become useless. However, Visual Studio does not start code analysis, simply using FxCopCmd. It performs code analysis reasonably ignoring asynchronous methods. (According to my research, it does not run any code analysis for asynchronous methods. This should be due to the problem in question.)

To get the same behavior as Visual Studio in CI assemblies, we can use fxcoptask.dll to run the FxCop analysis by code. Refer to this answer to learn how to integrate FxCop into an assembly. This will solve the problems mentioned in the problem. In addition, it provides many settings.

+2
source

Source: https://habr.com/ru/post/1012181/


All Articles