ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Redundant cast

Open wwh1004 opened this issue 5 years ago • 2 comments

Input code

void Method1() {
	int i4 = 0;
	void* i = null;
	void* result = (void*)((byte*)i4 - (byte*)i);
}

Erroneous output

void Method1()
{
	int i2 = 0;
	void* i = null;
	void* result = (void*)(IntPtr)(void*)((long)(IntPtr)(void*)((long)i2 - (long)i) / 1L);
}

Details

ilspy v6.2.0.6118-preview2

wwh1004 avatar Sep 22 '20 05:09 wwh1004

Which cast to you think is redundant? I guess there's an argument that the code should be:

void* result = (void*)((long)(IntPtr)(void*)((long)i2 - (long)i) / 1L);

as the last two casts are indeed completely redundant.

The intermediate cast through IntPtr is relevant to trim the result down to native int size. There's no decent other way of doing this when the C# 9 nint feature isn't available.

dgrunwald avatar Sep 22 '20 06:09 dgrunwald

could it be decompiled into 'i1* sub i1*' 'i2* sub i2*' by pattern matching, like (byte*)a - (byte*)b

wwh1004 avatar Sep 22 '20 06:09 wwh1004