These two warnings have been around for several weeks whenever I build
in Visual Studio:

\IBatisNet.DataMapper\TypeHandlers\BaseTypeHandler.cs(98,12): warning
CS0252: Possible unintended reference comparison; to get a value
comparison, cast the left hand side to type 'string'

\IBatisNet.DataMapper\TypeHandlers\UnknownTypeHandler.cs(130,12):
warning CS0252: Possible unintended reference comparison; to get a
value comparison, cast the left hand side to type 'string'

The offending code is:

 public virtual bool Equals(object obj, string str) 
 {
  if (obj == null || str == null) 
  {
   return obj == str; // <--- WARNING
  } 
  else 
  {
   object castedObject = ValueOf(obj.GetType(), str);
   return obj.Equals(castedObject);
  }
 }

The compiler recommends this:

 return (string)obj == str;

That doesn't look safe to me.

Changing the code to this:

 return obj == (object)str;

Makes the warning go away but ReSharper informs me that the cast is redundant.

Reply via email to