bogus "Typecast is redundant" notifications

I'm not sure how Reshaper can really avoid this, but in the code below, it
seems that the typecasts are NOT redundant (they serve an actual purpose).
Also it's interesting to note that ONE of the typecasts is not marked as
redundant... the cast to int16. All the others are flagged. However,
removing them would, I believe, break things. The return type is "object",
and this code is attempting to return specific types, so that the calling
code can do the appropriate things with it. Now, I'm not a C# wizard, so
maybe this code is bad or unnecessary and reshaper is actually correct...
well, except that it's not marking the int16 cast. Comments?

public new object this[int index]
{
get
{
System.Type columnType = Table.Columns[index].DataType;
if(IsNull(index))
{
//If the value is null and it is a value type then return a type
default
if(columnType.Equals(typeof(bool)))
return false;
else if(columnType.Equals(typeof(DateTime)))
return DateTime.MinValue;
else if(columnType.Equals(typeof(Decimal)))
return (Decimal)0;
else if(columnType.Equals(typeof(Double)))
return (Double)0;
else if(columnType.Equals(typeof(Guid)))
return Guid.Empty;
else if(columnType.Equals(typeof(Int16)))
return (Int16)0;
else if(columnType.Equals(typeof(Int32)))
return (Int32)0;
else if(columnType.Equals(typeof(Int64)))
return (Int64)0;
else if(columnType.Equals(typeof(Single)))
return (Single)0;
else if(columnType.Equals(typeof(string)))
return string.Empty;
else
return null;
}
else
{
if(columnType.Equals(typeof(string)))
return ((string)base[index]).Trim();
else
return base[index];
}
}
}


1 comment
Comment actions Permalink

We'll probably fix this bug in an bugfix release. Thanks!

--
Valentin Kipiatkov
Chief Scientist, Vice President of Product Development
JetBrains, Inc
http://www.jetbrains.com "Develop with pleasure!"


I'm not sure how Reshaper can really avoid this, but in the code
below, it seems that the typecasts are NOT redundant (they serve an
actual purpose). Also it's interesting to note that ONE of the
typecasts is not marked as redundant... the cast to int16. All the
others are flagged. However, removing them would, I believe, break
things. The return type is "object", and this code is attempting to
return specific types, so that the calling code can do the appropriate
things with it. Now, I'm not a C# wizard, so maybe this code is bad
or unnecessary and reshaper is actually correct... well, except that
it's not marking the int16 cast. Comments?

public new object this[int index]
{
get
{
System.Type columnType = Table.Columns[index].DataType;
if(IsNull(index))
{
//If the value is null and it is a value type then return a
type
default
if(columnType.Equals(typeof(bool)))
return false;
else if(columnType.Equals(typeof(DateTime)))
return DateTime.MinValue;
else if(columnType.Equals(typeof(Decimal)))
return (Decimal)0;
else if(columnType.Equals(typeof(Double)))
return (Double)0;
else if(columnType.Equals(typeof(Guid)))
return Guid.Empty;
else if(columnType.Equals(typeof(Int16)))
return (Int16)0;
else if(columnType.Equals(typeof(Int32)))
return (Int32)0;
else if(columnType.Equals(typeof(Int64)))
return (Int64)0;
else if(columnType.Equals(typeof(Single)))
return (Single)0;
else if(columnType.Equals(typeof(string)))
return string.Empty;
else
return null;
}
else
{
if(columnType.Equals(typeof(string)))
return ((string)base[index]).Trim();
else
return base[index];
}
}
}


0

Please sign in to leave a comment.