Search code examples
c#error-suppression

Find out what local variable requires this suppression


The c# project I am working with has "Warnings as Errors" turned on and they have lots of suppressions in the code. Among them the following:

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Maintainability"
, "CA1500:VariableNamesShouldNotMatchFieldNames"
, MessageId = "CS$<>8__locals1")]

If I remove the suppression, the build fails. How can I find out which specific identifier in the c# code causes the warning?


Solution

  • In this case, you can't. See that the MessageId value is "CS$<>8__locals1" - that's a C# compiler-generated local, so you won't see it in the C# source code directly - you'll have to use a tool like ILSpy or Reflector to see the CIL and the variable. According to this QA, the locals name means it's the name for a closure class variable - which means you're using a lambda function somewhere.

    However it should not be necessary to manually suppress this because FxCop is smart enough to know about C#-compiler generated locals and fields (fields or their enclosing types would be annotated with [CompilerGenerated] anyway). I'm guessing that your build process is using a recent C# compiler but a very old version of FxCop that does not recognise new C# language features.