7

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.)

Alex Angas
  • 59,219
  • 41
  • 137
  • 210

3 Answers3

6

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
  • 187,081
  • 35
  • 232
  • 306
  • @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 Sep 18 '09 at 16:10
  • @Alex: Try changing the category to "Microsoft.Globalization", see:- http://msdn.microsoft.com/en-us/library/ms182190.aspx – AnthonyWJones Sep 18 '09 at 16:23
  • 1
    You could try running FxCop, right-clicking the errors, and choosing Copy As SuppressMessage. That should get you the right attribute text. – TrueWill Sep 18 '09 at 17:38
  • 1
    Note that you have to compile with the CODE_ANALYSIS conditional then. – Frederik Gheysels Sep 21 '09 at 08:41
1

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
  • 11,510
  • 1
  • 38
  • 59
  • #pragma warning works for compiler warnings. Does it also work for code analysis? – OregonGhost Sep 18 '09 at 15:57
  • @OregonGhost #pragma warning(suppress) was introduced specifically for TFS code analysis. – Stu Mackellar Sep 18 '09 at 16:01
  • @JaredPar The question doesn't mention FxCop? – Stu Mackellar Sep 18 '09 at 16:01
  • @Stu it says Code Analysis warnings and mentions a specific category by name (Microsoft.Globalization) – JaredPar Sep 18 '09 at 16:03
  • @Stu: I've tried this with `#pragma warning(suppress: CA1305)` and no success. Ideas? – Alex Angas Sep 18 '09 at 16:05
  • 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 Sep 18 '09 at 23:38
  • 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 Sep 18 '09 at 23:41
1

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
  • 59,219
  • 41
  • 137
  • 210
  • Thank you, how would I disable StyleCop warnings SA1307 and SA1305? – Hamish Grubijan Mar 10 '11 at 18:18
  • @Hamish: I don't know about StyleCop. You might need to ask a new question for that. – Alex Angas Mar 10 '11 at 19:38
  • All right, I have done that. Just in case someone else will find this useful, here is a link to the question: http://stackoverflow.com/questions/5265154/how-to-disable-stylecop-warnings-sa1307-and-sa1305-around-a-struct – Hamish Grubijan Mar 10 '11 at 19:58