How to disable specific Code Analysis Warning for entire class
Asked Answered
P

3

17

I'm trying to disable a code analysis rule across an entire class, but NOT for the entire project, just a single class. In the example below, the build generates a CA1822 warning because it thinks that the unit test methods should be static.

The fix is to add the following attribute to each unit test method: [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]

However, that's cumbersome and clutters a class with many unit tests.

I've tried:

  1. Moving the attribute to the class
  2. Wrapping all of the methods in

#pragma warning disable CA1822

#pragma warning restore CA1822

Neither of these two approaches have worked.

public class TestClass
{
    public TestClass()
    {
        // some setup here
    }

    [Fact]
    public void My_Unit_Test1()
    {
        // the 'this' parameter is never used, causes CA warning 1822
    }

    [Fact]
    public void My_Unit_Test2()
    {
        // the 'this' parameter is never used, causes CA warning 1822
    }
}

Using VS2015 Update 2, .net 4.61, and the new Code Analysis Analyzers.

Preterit answered 7/7, 2016 at 13:21 Comment(5)
I addressed this (and other) issues by using a different Code Analysis ruleset for Unit Test projects. I turned off the rules that didn't apply to Unit Test projects.Sulphuryl
@MatthewWatson - I use a different ruleset for test projects as well. In this case, I want the rule to apply to my unit test project, just not one specific class.Preterit
Unfortunately you can't do this - the suppression must always apply to the level at which the warning/error is reported, so if that's a method, you can only suppress the warning for that method.Sulphuryl
@MatthewWatson - Do you know why the #pragma solution works for Stylecop but not for Code Analysis?Preterit
'Fraid not. Just different I guess.Sulphuryl
S
3

Right click the error under the error list tab, and you can choose either 'In Source' and 'In Suppression File'.

SuppressMessageAttibute will be added to the source code(method or class level) if you select 'In Source'.

'[assembly:SUppressMessage' will be added to GlobalSupressions.cs file, and you can config the 'Target' of the attribute.

snapshot here

Saltsman answered 21/11, 2017 at 6:13 Comment(1)
Unfortunately not all messages/warnings/errors in the Error List can be suppressed. Apparently the "Suppress > In Source / In Global Suppressions" menu only appears for IntelliSense-sourced messages/warnings/errors and not build-time messages/warnings/errors.Anabelle
D
3

Click the light bulb icon [💡] or press Ctrl + . combination. This should open a context menu like bellow.

suppression

Then select Suppress or Configure issues > Suppress XXX > in Suppression File menu item. It will create GlobalSuppression.cs file if not exists and add a new line to it.

By default it will only suppress warning for selected member (method). You can change Scope to type and Target to full class name (eg: Project.Domain.MyAwesomeClass).

Here's an example:

[assembly: SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This parameter of extension methods are always not null.", Scope = "type", Target = "Fonibo.Identity.Extensions.ClaimsIdentityExtension")]
Drug answered 19/7, 2020 at 22:17 Comment(0)
P
0

This is not exactly what you want but might be a lesser evil than the situation you have.

You can tell code analysis to ignore a particular class with the following attribute:

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]

Note that your project needs to have the Visual Studio option "Suppress results from generated code (managed only)" checked.

While annoying, you can still choose to run code analysis on the file if you temporarily comment the attribute.

Pictorial answered 7/7, 2016 at 14:8 Comment(1)
ExcludeFromCodeCoverage specifies that the attributed code should be excluded from code coverage information; not from code analysis. See the MSDN documentation (msdn.microsoft.com/en-us/library/…)Adam

© 2022 - 2024 — McMap. All rights reserved.