TK.
TK.

Reputation: 47873

FXCop Suppress Warning CA1800 (Unnecessary Casts)

I have the following code:

[SuppressMessage( "Microsoft.Performance", "CA1800:DoNotCastUnnecessarily" )]
private static void SetTestConnectionString( Component table )
{
    if( table is Object1 )
    {
        fn1( (Object1)table );
    }
    // ... a few more if statements for different Classes
}

However, when I run FxCop over this Class/Function it still generates the Warning

warning : CA1800 : Microsoft.Performance : 'table', a parameter, is cast to type 'xxx' multiple times in method 'ccc.SetTestConnectionString(Component)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction.

I know I could refactor this code to remove the warning, however it would make the code less readable. In this instance I would like to suppress this one message on this one function.

What am I doing wrong?

Upvotes: 3

Views: 3069

Answers (3)

Jagadeesan
Jagadeesan

Reputation: 243

I suspect your project file contains the DebugType is none. When set the DebugType is none, it will not detect the suppress code. So you can change the DebugType to full, because it will detect the suppress code properly.

<DebugType>full</DebugType>

Upvotes: 0

Anthony
Anthony

Reputation: 1

private static void SetTestConnectionString( Component table )
{
    if( table.GetType() == typeof(Object1) )
    {
        Object1 object1 = (Object1)table;
        fn1( object1 );
    }
    // ... a few more if statements for different Classes
}

Upvotes: 0

munissor
munissor

Reputation: 3785

Check if you defined the preprocessor symbol CODE_ANALYSIS in the properties of your project.

Have a look at: http://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.aspx

Upvotes: 5

Related Questions