Possible false positive for ValidateArgumentsOfPublicMethods

I added an == operator to a class.  Its method signature is

public static bool operator ==(Table table1, Table table2).

FxCop throws the ValidateArgumentsOfPublicMethods error and asks that I check the parameters for null.  OK, so I did.

if( table1 == null ||
    table2 == null )
{
  return false;
}

FxCop is now happy.  When I run my unit tests though, I get failures where the tests are throwing a StackOverflowException.  Upon further analysis, it appears that testing for null within the == operator introduces an infinite recursion situation.

What is the best way to handle this



Answer this question

Possible false positive for ValidateArgumentsOfPublicMethods

  • WalidDib

    This is definitely a good approach, just thought I'd point out that the BCL already provides such a useful static helper:

    if (Object.Equals(table1, null)) return false;

    etc

    Michael



  • RennySchweiger

    Since your == operator overloading on a reference type should have actually raised another violation; DoNotOverloadOperatorEqualsOnReferenceTypes, I don't think that FxCop should have given any other advice.



  • rpjimenez

    That is a great solution!

    Although it appears to solve my problem, should this situation be considered a false positive for ValidateArgumentsOfPublicMethods as it is currently implemented   

    If it flags this situation as a violation of the rule, should it not give advice similar to what you suggested above (only within the == operator method though)


  • mfroman

    I had the same problem and created a static method that checks for the null value. Because it takes an object, the == operator of the class is not used:



    private static bool IsNull(object table1)
    {
        return (null == table1);
    }

    public static bool operator ==(Table table1, Table table2)
    {
        if (IsNull(table1)) return false;
        if (IsNull(table2)) return false;
        ...
    }

     



  • Yin He

    So my situation is actually a false negative for the DoNotOverloadOperatorEqualsOnReferenceTypes rule Big Smile

    I sure like FxCop.  I learn alot about coding each time I use it.

    Thanks.


  • Possible false positive for ValidateArgumentsOfPublicMethods