roslyn: No warning for unreachable code after throw expression

Version Used: 2.3.0.61719 (5fbdd5c5)

Steps to Reproduce:

using System;

class X
{
    public static void Main ()
    {
        var x = true ? throw new NullReferenceException () : 1;
        x = 2; // This code is unreachable but no info about it
        return;
    }
}

Expected Behavior:

Proper warning about unreachable code

Actual Behavior:

No warning.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (19 by maintainers)

Most upvoted comments

@CyrusNajmabadi consider following sample (it’s just same code shaped differently)

using System;

class X
{
    int TestExpr (out int z)
    {
        return true ? throw new NullReferenceException () : 1;
    }
}

Now if C# compiler would really follow your statement of

There is no language in the C# spec about reachability of subexpressions, only reachability of statements. So this is not an actual bug/issue with Roslyn.

then this code should fail to compile because z is not always assigned to before control leaves the current method but it compiles fine.