4

The fxcop analysis gives the CA2202 warning for the following method body over the foreach line:

public void LogAnalysis(IEnumerable<string> steps, bool append = false)
{
    if (steps != null)
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            foreach (string step in steps) // this is line 34
            {
                sb.AppendLine(step);
            }
            if (append)
            {
                sb.Insert(0, string.Format(
                     CultureInfo.InvariantCulture, 
                    "__________Logging started at {0}__________\n",
                     DateTime.Now.ToString(CultureInfo.InvariantCulture)));

                File.AppendAllText(AnalysisLogFile, sb.ToString());
            }
            else
            {
                File.WriteAllText(AnalysisLogFile, sb.ToString());
            }
        }
        catch (Exception e) when (e is IOException || e is UnauthorizedAccessException)
        {
            LogError(e.Message);
        }
        sb.Clear();
    }
}

Warning CA2202 Object 'steps.GetEnumerator()' can be disposed more than once in method 'LoggingService.LogAnalysis(IEnumerable, bool)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 34

I have done some research and seen that nested using statements and Dispose calls cause the analyzer to freak and give this warning, but I do have neither an explicit Dispose call nor a using block. I have not encountered another case where this warning pops for the foreach loop. I know how to suppress the warning but I just wanted to understand what could be the cause of this?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
mcy
  • 1,209
  • 6
  • 25
  • 37
  • 1
    `foreach` includes an implicit disposal step. But I don't see why that code would cause this warning. – Jon Skeet Oct 08 '19 at 11:54
  • 3
    Which FxCop are you using? The old one that does binary analysis, or the new one built on top of Roslyn Analyzers (i.e. the `Microsoft.CodeAnalysis.FxCopAnalyzers` NuGet package) – canton7 Oct 08 '19 at 12:01
  • Side note. You want to do your insert at 0 before, otherwise you're wasting a huge amount of the performance of string builder and really only getting the benefit of not creating new strings – Jlalonde Oct 08 '19 at 12:19
  • @Jlalonde thanks for the heads up. I append the first line only if `append` is true and call different methods (`wrietalltext` vs `appendalltext`) based on `append` as well, so I could not figure out a better way to do that; I either had to repeat `foreach` lines in the `if-else` or shift the contents of the builder. I think I will revert to repeating the loop I guess. – mcy Oct 08 '19 at 12:24
  • Yep, the Analyze menu runs the old binary analysis, which is horrifically out of date and quite broken in a few cases. The analyzer-based one just runs on build (or sometimes in the background?) – canton7 Oct 08 '19 at 12:41

1 Answers1

0

Based on the comments of canton7, I realized that when you force the code analysis using the menu Analyze>Run Code Analysis, it forces using the old binary fxcop even if you have installed the new Roslyn Nuget Package. The new analyzers are used automatically on build, and when I have used the new ones the warning mentioned in the question have vanished.

mcy
  • 1,209
  • 6
  • 25
  • 37