BLOG

Suppressing Code Quality Violations Found by SPCAF

2 min read
Blog Header Developer

When analyzing SharePoint solutions and apps, sometimes the analysis reports show potential issues which are implemented that way on purpose. Still you might want to keep the entire SPCAF rule enabled as you would like to find other occurrences of the issue.

Hence many of our users asked us to add a functionality to allow to suppress a single warning in the code and since SPCAF version 5.2 this is now possible for managed code in assemblies.

Note: Individual suppression of errors and warnings in XML, CSS, JavaScript or HTML code are currently not possible.

To suppress SPCAF warnings in assemblies SPCAF makes use of the attribute “System.Diagnostics.CodeAnalysis.SuppressMessage” of the .NET framework.

The attribute takes 2 arguments

  • for the category of the suppressed rule and 
  • for the checkid.

The checkid (second parameter) can be used either in a short form (only Id, eg:”SPC050235″) and a long form (Id and the classname of the rule, eg. “SPC050235:ApplyLockForCachingObjects”).
SPCAF further interprets a third optional arguments called “Justification”, which allows you to explain why you have to suppress this violation.

.All other arguments of”System.Diagnostics.CodeAnalysis.SuppressMessage” (MessageId, Scope, Target) are currently not evaluated by SPCAF.

Example:

[SuppressMessage("SPCAF.Rules.BestPractice", "SPC050235:ApplyLockForCachingObjects", Justification = "ApplyLock not needed in this case.")]

Important: The SuppressMessage attribute is a conditional attribute that is included in the IL metadata of your managed code assembly only if the CODE_ANALYSIS compilation symbol has been defined.

How to suppress warnings for a single method

The following code snippet illustrates how you can suppress all warnings for the rule “SPC050235 ApplyLockForCachingObjects” for a single method. It also shows how to provide a justification for the suppression.

Note: All occurrences of the warning for the given rule in this method are suppressed. This could lead to unexpected results if just a single warning should be suppressed. Due to the limitations of .NET code analysis attribute it is not possible to pinpoint just a single occurrence.

#define CODE_ANALYSIS 
using System.Diagnostics.CodeAnalysis; 
... 
namespace SuppressExample 
{ 
  public class ApplyLock 
  { 
    [SuppressMessage("SPCAF.Rules.BestPractice", "SPC050235:ApplyLockForCachingObjects", Justification = "ApplyLock removed.")] 
    public void MethodWithoutApplyLock() 
    { 
      ... 
    } 
  } 
}

How to suppress warnings for a entire class

In case you want to suppress a warning for an entire class, you can position the attribute before the class declaration.

The following code snippet illustrates how this is done.

#define CODE_ANALYSIS 
using System.Diagnostics.CodeAnalysis; 
... 
namespace SuppressExample 
{ 
  [SuppressMessage("SPCAF.Rules.BestPractice", "SPC050235:ApplyLockForCachingObjects")] 
  public class ApplyLock 
  { 
  ... 
  } 
}

Summary

In this little How-To you learned how to suppress managed code quality violations reported by SPCAF in a single method or an entire class. In order to benefit from this new feature you require SPCAF v5.2 or later.

Matthias Einig

Matthias is a Microsoft MVP, co-founder, and CEO at Rencore. His mission is to helps enterprises stay in control of their Microsoft collaboration technology. Matthias and his team achieve this by providing insights, advice, and actionable ways to manage platform growth. Matthias is speaking at many conferences and community events all over the world and is one of the organizers of the European Collaboration Summit.