Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper warning casting enum to UIntPtr, but no compiler warning

In the code below, Resharper gives me a warning: Cannot cast expression of type 'Color' to type 'UIntPtr'. (Actually, Resharper thinks it's an actual error.)

However, there is no compiler warning and it works fine.

This looks like a Resharper bug to me. Is it? Or is there something bad about it that the compiler isn't worrying about? (I'm using Resharper 7.1.1)

using System;

namespace Demo
{
    internal class Program
    {
        public enum Color { Red, Green, Blue }

        private static void Main(string[] args)
        {
            UIntPtr test = (UIntPtr) Color.Red; // Resharper warning, no compile warning.
        }
    }
}

I can make the warning go away by casting the value to an int first, so I have a workaround:

UIntPtr test = (UIntPtr)(int) Color.Red;
like image 972
Matthew Watson Avatar asked Oct 22 '22 15:10

Matthew Watson


1 Answers

This looks like a Resharper bug to me. Is it?

Yes :

RSRP-78748 False 'conversion does not exist' (UIntPtr)

using System;

class A
{
    static void Main()
    {
        E? x = 0;
        UIntPtr z = (UIntPtr)x;
    }
}
enum E { }

It is a known spec devation.

Not fixed as of 2013-03-05.

like image 153
AakashM Avatar answered Nov 03 '22 20:11

AakashM