Jarodtweiss

Hello All,

I have a class where I want to define some messages. I know this is a performance issue but I want to code it like that :

namespace MyNs

{

public static class Msg

{

public static readonly string Msg1 = "My Msg";

}

}

So when I run the code analysis I receive the error message CA1802 saying that I should transform the static readonly into const. I want to suppress this message but I do not want to put it on the member Msg1 cause I have several like that.

I have tried the following Suppression but none work:

[SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Scope = "type", Target = "MyNs.Msg")]

[SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Scope = "member", Target = "MyNs.Msg")]

[assembly: SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Scope = "type", Target = "MyNs.Msg")]

[assembly: SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Scope = "member", Target = "MyNs.Msg")]

Do you have any idea of how I can suppress this message (I do not want to suppress it globally in the project).

Thanks,




Re: Visual Studio Code Analysis and Code Metrics Unable to suppress some warnings

Jarodtweiss

No idea anyone




Re: Visual Studio Code Analysis and Code Metrics Unable to suppress some warnings

David M. Kean - MSFT

SuppressMessage can only suppress a single instance a violation, not multiple violations, so you will need as many suppressions as you have violations.

To suppress this instance, you have a choice, either suppress in-source or as a global suppression.

For example:

In-Source Suppression



namespace MyNs
{
    public static class Msg
    {
        [SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate")]
        public static readonly string Msg1 = "My Msg";

        [SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate")]
        public static readonly string Msg2 = "My Msg";
    }
}


 

Global Suppression



[assembly: SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Scope = "member", Target = "MyNs.Msg.Msg1")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate", Scope = "member", Target = "MyNs.Msg.Msg2")]

namespace MyNs
{
    public static class Msg
    {       
        public static readonly string Msg1 = "My Msg";
       
        public static readonly string Msg2 = "My Msg";
    }
}


 

Regards

David






Re: Visual Studio Code Analysis and Code Metrics Unable to suppress some warnings

Jarodtweiss

Oh ok thanks !

And do u know if this is an improvement planned for a next version

Regards,

Pierre-Emmanuel






Re: Visual Studio Code Analysis and Code Metrics Unable to suppress some warnings

David M. Kean - MSFT

Not at this stage. The problem with allowing multiple warnings to be suppressed by a single SuppressMessageAttribute, is that it also suppresses any warnings in any new code that is added in the future.

This is particularly bad with security warnings. New security flaws could be introduced without making the user aware that the new code that they just wrote exposed a giant security hole.