Resharper complains about the following code, saying that the last null check is redundant as the 'expression is always false':
ICloneable data = item as ICloneable;
if (data == null)
throw new InvalidCastException("blah blah, some error message");
object copy = data.Clone();
if (copy == null) // <-- this is where it complains.
return default(T);
How does it know it can never be null?
ReSharper assumes that your object adheres to the contract of
ICloneable, which says among other things thatFrom the fact that
datais checked to be non-null and the assumption that you return an object of the same or compatible type from your implementation ofICloneable.Clone()ReSharper concludes thatcopyis also non-null, triggering the warning.Of course it is definitely possible to return
nullfromClone. However, returningnullwould be a coding error, so it is a good idea to skip that null check.