Search code examples
c#resharperresharper-7.1

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;

Solution

  • 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.