0

I have a simple utility method that takes a decimal inch value and returns a fraction (i.e. turns 1.5 into 1 1/2") for display purposes. This is working just fine but Resharper is telling me that every case in my switch is 'Heuristically Unreachable' and giving a warning, I can't figure out why. Here is the method:

static string FractionalInches(double decimalInches)
{
    //Set initial variables
    string fraction = "";

    //Round to an integer for full inches
    int test = (int)Math.Round(decimalInches, 0);

    if (Math.Abs(decimalInches-test) < SharedData.FloatPointTolerance) //Even inches, no fractions here
    { return test.ToString(); }

    //Has a fraction
    var fullInches = decimalInches - test < 0 ? test - 1 : test;

    //get the fraction - rounded to 16ths.
    double sixteenths = Math.Abs(decimalInches - test) * 16;
    int sixt = (int)Math.Round(sixteenths, 0);

    switch (sixt)
    {
        case 1:
            fraction = "1/16";
            break;
        case 2:
            fraction = "1/8";
            break;
        case 3:
            fraction = "3/16";
            break;
        case 4:
            fraction = "1/4";
            break;
        case 5:
            fraction = "5/16";
            break;
        case 6:
            fraction = "3/8";
            break;
        case 7:
            fraction = "7/16";
            break;
        case 8:
            fraction = "1/2";
            break;
        case 9:
            fraction = "9/16";
            break;
        case 10:
            fraction = "5/8";
            break;
        case 11:
            fraction = "11/16";
            break;
        case 12:
            fraction = "3/4";
            break;
        case 13:
            fraction = "13/16";
            break;
        case 14:
            fraction = "7/8";
            break;
        case 15:
            fraction = "15/16";
            break;
        case 16:
            fraction = "";
            fullInches += 1;
            break;
    }

    //construct and return the final string
    return fraction == "" ? fullInches.ToString() : fullInches + " " + fraction;
}

It gives me the unreachable warning on every case in the switch statement, but I know it gets hit because I can send in decimal values and they return the correct fractions... Anyone know why R# is giving this warning? I can ignore it easy enough but don't understand why it's there in the first place.

sfaust
  • 2,089
  • 28
  • 54
  • Which version of R# and .NET are you using so far? Is there any specific R# options? Because I didn't see any warnings in my end – Pavel Anikhouski Apr 24 '20 at 14:17
  • Latest R# Ultimate (2020.1) inside VS 2019 Community. Project is referencing .NET 4.6. No special R# options that I'm aware of... – sfaust Apr 24 '20 at 14:22
  • 1
    I've updated my R# to the latest version and get the same warning. After moving the entire `switch` to the local function it goes away. It seems, that this feature was [added int the latest version](https://www.jetbrains.com/resharper/whatsnew/), new dataflow for integer values, like _Heuristically unreachable switch cases checking int values._ – Pavel Anikhouski Apr 24 '20 at 15:07
  • Interesting. I'm still not sure I completely understand why that would be deemed unreachable. However, after looking at that and doing some more research I got it to go away. As you said if I move the case to a new method and pass the int it seems to go away (which is quite strange). I also found [this] (https://stackoverflow.com/questions/10754251/converting-a-double-to-an-int-in-c-sharp). After looking at that I changed the line `int sixt = (int)Math.Round(sixteenths, 0);` to `int sixt = Convert.ToInt32(sixteenths);` and it went away. Want to make that an answer so I can accept? – sfaust Apr 24 '20 at 16:21
  • cast to `int` will truncate the decimal value, when `Convert.ToInt32` not. It seems to be a main point here. However, it's better to ask JB support about exact behavior here. I don't think that my workaround answers this question:) – Pavel Anikhouski Apr 24 '20 at 16:45
  • 1
    Right, but originally I rounded it to 0 decimal places THEN truncated it. Effectively, this is the same as using `Convert.ToInt32`, just not as clean. I'm guessing that R# is objecting to the cast thinking it's a possible truncation, but I will talk to them about it... Thank you for your help! – sfaust Apr 24 '20 at 17:02
  • 1
    There is an issue in the latest ReSharper related to the integer dataflow analysis and the `Math.Round` - https://youtrack.jetbrains.com/issue/RSRP-479314. It is marked as Fixed, and the fix will be available in the upcoming 2020.1.2 bugfix. – Alexander Kurakin May 04 '20 at 12:36

0 Answers0