Invalid 'Redundant type cast' message bug

Hi,

I'm having a situation where resharper (1.5.165.14) says a typecast is
redundant, however removing the typecast causes different functionality
(invalid code in this case).

- I have a proxy class with a read-only innerList class both
implementing IList.

The IList specifies a this[int index] get; and set; property, but is
implicitly implemented with only a get:

public class ReadOnlyList : IList
{
object IList.this[int index]
{
get { return this[index]; }
set { throw new NotImplementedException("list is read only"); }
}

public object this[int index]
{
get
{ // Gets the value from the list
return null;
}
}

// ... other members
}

in order to delegate the IList set; property, I need to cast it to an
IList, however Resharper tells me the IList cast is redundant:

public class ReadOnlyListProxy : IList
{
private ReadOnlyList innerList = new ReadOnlyList();

public object this[int index]
{
get { return innerList[index]; }
}

public object IList.this[int index]
{
get { return innerList[index]; }
set { ((IList) innerList)[index] = value; }
}

// ... other members
}

Don't know if this is already solved in Resharper EAP...

Note: this code isn't a direct reproduction of the problem, it only
occurs if the source code for ReadOnlyList is not available... (I think)

If it isn't reproducible like this, I can send my original code.

Thanks, Wiebe

9 comments
Comment actions Permalink

This issue is definetely fixed in 2.0

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Wiebe Tijsma" <wiebeREMOVE@CAPITALStijsma.com> wrote in message
news:dfkcd2$qsf$1@is.intellij.net...

Hi,

>

I'm having a situation where resharper (1.5.165.14) says a typecast is
redundant, however removing the typecast causes different functionality
(invalid code in this case).

>

- I have a proxy class with a read-only innerList class both implementing
IList.

>

The IList specifies a this[int index] get; and set; property, but is
implicitly implemented with only a get:

>

public class ReadOnlyList : IList
{
object IList.this[int index]
{
get { return this[index]; }
set { throw new NotImplementedException("list is read only"); }
}

>

public object this[int index]
{
get
{ // Gets the value from the list
return null;
}
}

>

// ... other members
}

>

in order to delegate the IList set; property, I need to cast it to an
IList, however Resharper tells me the IList cast is redundant:

>

public class ReadOnlyListProxy : IList
{
private ReadOnlyList innerList = new ReadOnlyList();

>

public object this[int index]
{
get { return innerList[index]; }
}

>

public object IList.this[int index]
{
get { return innerList[index]; }
set { ((IList) innerList)[index] = value; }
}

>

// ... other members
}

>

Don't know if this is already solved in Resharper EAP...

>

Note: this code isn't a direct reproduction of the problem, it only occurs
if the source code for ReadOnlyList is not available... (I think)

>

If it isn't reproducible like this, I can send my original code.

>

Thanks, Wiebe



0
Comment actions Permalink

Eugene Pasynkov (JetBrains) wrote:

This issue is definetely fixed in 2.0

OK, Thanks...

0
Comment actions Permalink

Hello Eugene,

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> schrieb im
Newsbeitrag news:dfm3g1$due$1@is.intellij.net...

This issue is definetely fixed in 2.0


I think there is still a problem with redundant type casts in 206: if you
have an overloaded method and want to pass null of a specific type, this
type cast is marked redundant by ReSharper.

Example:

class C {
void Do(A a) { }
void Do(B b) { }
}

In a unit test you would like to call
Do((A)null);


Sincerely,
Stefan Lieser


0
Comment actions Permalink

Cannot reproduce this issue.
Please could you provide small example

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Stefan Lieser" <slieser@t-online.de> wrote in message
news:dgbik2$hvp$1@is.intellij.net...

Hello Eugene,

>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> schrieb im
Newsbeitrag news:dfm3g1$due$1@is.intellij.net...

>> This issue is definetely fixed in 2.0
>

I think there is still a problem with redundant type casts in 206: if you
have an overloaded method and want to pass null of a specific type, this
type cast is marked redundant by ReSharper.

>

Example:

>

class C {
void Do(A a) { }
void Do(B b) { }
}

>

In a unit test you would like to call
Do((A)null);

>
>

Sincerely,
Stefan Lieser



0
Comment actions Permalink

Hello Eugene,

ok, you're right, I should have posted a proved example. So here it is:


using NMock;
using NUnit.Framework;

namespace CastNull {
interface ICommand {
void Command(object obj);
}

public class A {
public void Do() { }
}


public class CastNull {
ICommand cmd;
DynamicMock mock;


public void Test() {
mock.Expect("Command", (A)null); // ***** This cast is the
critical point
cmd.Command(null);
mock.Verify();
}


public void Setup() {
mock = new DynamicMock(typeof(ICommand));
cmd = mock.MockInstance as ICommand;
}
}
}


If you do not cast the null to type A, NMock didn't find method "Command":

-


Test started: Assembly: CastNull.dll -


TestCase 'CastNull.CastNull.Test' failed: System.MissingMethodException :
method ]]> not defined
at NMock.DynamicMock.checkMethodIsValidIfNoConstraints(MethodSignature
signature)
at NMock.DynamicMock.getMethod(MethodSignature signature)
at NMock.Mock.addExpectation(String methodName, MockCall call)
at NMock.Mock.ExpectAndReturn(String methodName, Object result, Object[]
args)
at NMock.Mock.Expect(String methodName, Object[] args)
c:\code\playground\current\castnull\castnull.cs(23,0): at
CastNull.CastNull.Test()


Sincerely,
Stefan Lieser


0
Comment actions Permalink

Still not reproducable.
I've tried your example with latest NUnit build (2.2.2), and the test
succeed both woth casted null and non-cansted null.

Moreover, casting of null is always safe operation - passed as parameter, it
will loose it type again

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Stefan Lieser" <slieser@t-online.de> wrote in message
news:dgol0v$2j4$1@is.intellij.net...

Hello Eugene,

>

ok, you're right, I should have posted a proved example. So here it is:

>
>

using NMock;
using NUnit.Framework;

>

namespace CastNull {
interface ICommand {
void Command(object obj);
}

>

public class A {
public void Do() { }
}

>


public class CastNull {
ICommand cmd;
DynamicMock mock;

>


public void Test() {
mock.Expect("Command", (A)null); // ***** This cast is the
critical point
cmd.Command(null);
mock.Verify();
}

>


public void Setup() {
mock = new DynamicMock(typeof(ICommand));
cmd = mock.MockInstance as ICommand;
}
}
}

>
>

If you do not cast the null to type A, NMock didn't find method "Command":

>

------ Test started: Assembly: CastNull.dll ------
TestCase 'CastNull.CastNull.Test' failed: System.MissingMethodException :
method <Command> not defined
at NMock.DynamicMock.checkMethodIsValidIfNoConstraints(MethodSignature
signature)
at NMock.DynamicMock.getMethod(MethodSignature signature)
at NMock.Mock.addExpectation(String methodName, MockCall call)
at NMock.Mock.ExpectAndReturn(String methodName, Object result, Object[]
args)
at NMock.Mock.Expect(String methodName, Object[] args)
c:\code\playground\current\castnull\castnull.cs(23,0): at
CastNull.CastNull.Test()

>
>

Sincerely,
Stefan Lieser

>



0
Comment actions Permalink

Hello Eugene,

my versions:

NUnit 2.2.2
NMock 1.1 (the dll file version shows 1.0.1536.20687)


Sincerely,
Stefan Lieser


"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> schrieb im
Newsbeitrag news:dgp3ij$rej$1@is.intellij.net...

Still not reproducable.
I've tried your example with latest NUnit build (2.2.2), and the test
succeed both woth casted null and non-cansted null.

>

Moreover, casting of null is always safe operation - passed as parameter,
it will loose it type again

>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Stefan Lieser" <slieser@t-online.de> wrote in message
news:dgol0v$2j4$1@is.intellij.net...

>> Hello Eugene,
>>
>> ok, you're right, I should have posted a proved example. So here it is:
>>
>>
>> using NMock;
>> using NUnit.Framework;
>>
>> namespace CastNull {
>> interface ICommand {
>> void Command(object obj);
>> }
>>
>> public class A {
>> public void Do() { }
>> }
>>
>>
>> public class CastNull {
>> ICommand cmd;
>> DynamicMock mock;
>>
>>
>> public void Test() {
>> mock.Expect("Command", (A)null); // ***** This cast is the
>> critical point
>> cmd.Command(null);
>> mock.Verify();
>> }
>>
>>
>> public void Setup() {
>> mock = new DynamicMock(typeof(ICommand));
>> cmd = mock.MockInstance as ICommand;
>> }
>> }
>> }
>>
>>
>> If you do not cast the null to type A, NMock didn't find method
>> "Command":
>>
>> -


Test started: Assembly: CastNull.dll -


>> TestCase 'CastNull.CastNull.Test' failed: System.MissingMethodException :
>> method not defined >> at NMock.DynamicMock.checkMethodIsValidIfNoConstraints(MethodSignature >> signature) >> at NMock.DynamicMock.getMethod(MethodSignature signature) >> at NMock.Mock.addExpectation(String methodName, MockCall call) >> at NMock.Mock.ExpectAndReturn(String methodName, Object result, Object[] >> args) >> at NMock.Mock.Expect(String methodName, Object[] args) >> c:\code\playground\current\castnull\castnull.cs(23,0): at >> CastNull.CastNull.Test() >> >> >> Sincerely, >> Stefan Lieser >> >> >]]>



0
Comment actions Permalink

Hello Eugene,

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> schrieb im
Newsbeitrag news:dgp3ij$rej$1@is.intellij.net...

Still not reproducable.
I've tried your example with latest NUnit build (2.2.2), and the test
succeed both woth casted null and non-cansted null.

>

Moreover, casting of null is always safe operation - passed as parameter,
it will loose it type again


I did some more tests on the cast topic. Compile the code below and look at
the IL code.

Having a method

void F(params object[] args) {}

If you call F(null), args will be null. If you call F((A)null), args will be
an empty array. So it is an error if ReSharper states "Type cast is
redundant".


Sincerely,
Stefan Lieser

C# code:----


using NUnit.Framework;

namespace CastNull
{
public class A {
public void Do() {
}
}


public class CastNull
{

public void CastedNull() {
CheckParameter((A)null);
}


public void Null() {
CheckParameter(null);
}

private void CheckParameter(params object[] args) {
Assert.IsNotNull(args);
Assert.AreEqual(1, args.Length);
Assert.IsNull(args[0]);
}
}
}
-



.method public hidebysig instance void Null() cil managed
{
.custom instance void
NUnit.Framework.TestAttribute::.ctor() = ( 01 00 00 00 )
// Codegröße 8 (0x8)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: call instance void
CastNull.CastNull::CheckParameter(object[])
IL_0007: ret
} // end of method CastNull::Null

-


.method public hidebysig instance void CastedNull() cil managed
{
.custom instance void
NUnit.Framework.TestAttribute::.ctor() = ( 01 00 00 00 )
// Codegröße 15 (0xf)
.maxstack 2
.locals init ( object[] CS$00000002$00000000)
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: newarr System.Object
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: call instance void
CastNull.CastNull::CheckParameter(object[])
IL_000e: ret
} // end of method CastNull::CastedNull




0
Comment actions Permalink

I see.
Thank you for detailed clarification.
This issue is fixed in 207

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Stefan Lieser" <slieser@t-online.de> wrote in message
news:dgqtd8$1o9$1@is.intellij.net...

Hello Eugene,

>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> schrieb im
Newsbeitrag news:dgp3ij$rej$1@is.intellij.net...

>> Still not reproducable.
>> I've tried your example with latest NUnit build (2.2.2), and the test
>> succeed both woth casted null and non-cansted null.
>>
>> Moreover, casting of null is always safe operation - passed as parameter,
>> it will loose it type again
>

I did some more tests on the cast topic. Compile the code below and look
at the IL code.

>

Having a method

>

void F(params object[] args) {}

>

If you call F(null), args will be null. If you call F((A)null), args will
be an empty array. So it is an error if ReSharper states "Type cast is
redundant".

>
>

Sincerely,
Stefan Lieser

>

C

  1. code:------------------------------------------------

using NUnit.Framework;

>

namespace CastNull
{
public class A {
public void Do() {
}
}

>


public class CastNull
{

public void CastedNull() {
CheckParameter((A)null);
}

>


public void Null() {
CheckParameter(null);
}

>

private void CheckParameter(params object[] args) {
Assert.IsNotNull(args);
Assert.AreEqual(1, args.Length);
Assert.IsNull(args[0]);
}
}
}
-------------------------------------------------

>

.method public hidebysig instance void Null() cil managed
{
.custom instance void
NUnit.Framework.TestAttribute::.ctor() = ( 01 00 00 00 )
// Codegröße 8 (0x8)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: call instance void
CastNull.CastNull::CheckParameter(object[])
IL_0007: ret
} // end of method CastNull::Null

>

-------------------------------------------------
.method public hidebysig instance void CastedNull() cil managed
{
.custom instance void
NUnit.Framework.TestAttribute::.ctor() = ( 01 00 00 00 )
// Codegröße 15 (0xf)
.maxstack 2
.locals init ( object[] CS$00000002$00000000)
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: newarr System.Object
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: call instance void
CastNull.CastNull::CheckParameter(object[])
IL_000e: ret
} // end of method CastNull::CastedNull

>
>



0

Please sign in to leave a comment.