Gendarme - CallingEqualsWithNullArgRule

2 views
Skip to first unread message

Nidhi Rawal

unread,
Aug 13, 2007, 2:48:30 PM8/13/07
to mono-soc-2007
Description:
The method calls Object.Equals (Object) with null as argument. The
null value as argument should always return false.

Examples:
1) public class CallingEqualsWithNullArg
{
public static void Main (string [] args)
{
CallingEqualsWithNullArg c = new
CallingEqualsWithNullArg ();
c.Equals (null);
}
}
This is bad.

2) public class CallingEqualsWithoutNullArg
{
public static void Main (string [] args)
{
CallingEqualsWithoutNullArg c = new
CallingEqualsWithoutNullArg ();
CallingEqualsWithoutNullArg c1 = new
CallingEqualsWithoutNullArg ();
c.Equals (c2);
}
}
This is good.

3) public class OverridingEqualsAndPassingNullArg
{
public override bool Equals (Object obj)
{
return this == null
}
public override int GetHashCode ()
{
return 1;
}
public static void Main (string [] args)
{
OverridingEqualsAndPassingNullArg o = new
OverridingEqualsAndPassingNullArg ();
o.Equals (null);
}
}
This is bad.

4) public class ImplementingNewEqualsAndPassingNullArg
{
public new bool Equals (Object obj)
{
if (obj == null)
return false;
else
return this == obj;
}
public static void Main (string [] args )
{
ImplementingNewEqualsAndPassingNullArg i = new
ImplementingNewEqualsAndPassingNullArg ();
i.Equals (null);
}
}
This should not make difference.

Reference:
http://findbugs.sourceforge.net/bugDescriptions.html#EC_NULL_ARG

Sebastien

unread,
Aug 14, 2007, 1:37:28 PM8/14/07
to mono-soc-2007

Is there any reason the rule shouldn't also handle overloads ?
e.g. bool Rule.Equals (Rule rule)

Sebastien

Nidhi Rawal

unread,
Aug 17, 2007, 6:59:30 AM8/17/07
to mono-soc-2007
Reply all
Reply to author
Forward
0 new messages