views:

671

answers:

3

How do I ignore a specific VS code analysis rule (say CA1305 : Microsoft.Globalization) within a:

  • Method?
  • Class?
  • Namespace?

(Assuming these options are all possible.)

+1  A: 

Use #pragma warning(suppress: Cxxxx)

You can put the pragma at the appropriate scope in the source file (i.e. class, method)

See http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx

Stu Mackellar
#pragma warning works for compiler warnings. Does it also work for code analysis?
OregonGhost
This doesn't work for FxCop rules
JaredPar
@OregonGhost #pragma warning(suppress) was introduced specifically for TFS code analysis.
Stu Mackellar
@JaredPar The question doesn't mention FxCop?
Stu Mackellar
@Stu it says Code Analysis warnings and mentions a specific category by name (Microsoft.Globalization)
JaredPar
@Stu: I've tried this with `#pragma warning(suppress: CA1305)` and no success. Ideas?
Alex Angas
From the docs: "suppress is only supported for C6000 warnings (code analysis warnings), which are enabled with the /analyze (Enterprise Code Analysis) compiler option." You could use #pragme warning(disable) instead, or alternatively use the SuppressMessage attribute as @AnthonyWJones suggests.
Stu Mackellar
Also:If the #pragma warning directive with the suppress warning specifier does not suppress the warning, it might be because the particular warning applies to the function body as a whole, not its individual statements. To suppress warnings of this type, such as Warning 28195, place the #pragma warning directive on the line immediately preceding the first brace ({) of the function body.
Stu Mackellar
+2  A: 

You can use the SupressMessage attribute like this:-

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2233:OperationsShouldNotOverflow", MessageId = "newValue+1", Justification = "The reason I think its acceptable in this case")]
void SomeMethod()
{
   // Some code that would normal cause this Code Analysis message
}

On a method, property, type etc.

AnthonyWJones
@AnthonyWJones: I tried `[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1305:Microsoft.Globalization")]` (for brevity) on a method but still the warning showed. Any ideas?
Alex Angas
@Alex: Try changing the category to "Microsoft.Globalization", see:-http://msdn.microsoft.com/en-us/library/ms182190.aspx
AnthonyWJones
You could try running FxCop, right-clicking the errors, and choosing Copy As SuppressMessage. That should get you the right attribute text.
TrueWill
Note that you have to compile with the CODE_ANALYSIS conditional then.
Frederik Gheysels
+1: This worked when I used @TrueWill's suggestion
Alex Angas
A: 

I downloaded FXCop as suggested by @TrueWill's comment on @AnthonyWJones' answer. This gave me the SuppressMessage:

[SuppressMessage("Microsoft.Globalization",
    "CA1305:SpecifyIFormatProvider",
    MessageId = "System.String.Format(System.String,System.Object)")]

This was far harder than it should have been. What happened to that FXCop integration into Visual Studio? Thanks to the answerers for their help.

Alex Angas