Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore code analysis rules in Visual Studio

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

like image 375
Alex Angas Avatar asked Sep 18 '09 15:09

Alex Angas


People also ask

How do I turn off code analysis in Visual Studio?

To open this page, right-click the project node in Solution Explorer and select Properties. Select the Code Analysis tab. To disable source analysis at build time, uncheck the Run on build option. To disable live source analysis, uncheck the Run on live analysis option.

How do I ignore warnings in C#?

Use a #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code.


2 Answers

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.

like image 59
AnthonyWJones Avatar answered Nov 14 '22 20:11

AnthonyWJones


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

like image 45
Stu Mackellar Avatar answered Nov 14 '22 21:11

Stu Mackellar