Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreachable "else block" code isn't reported as such, when "is var" is used #15489

Closed
DavidArno opened this issue Nov 23, 2016 · 4 comments
Closed
Assignees
Labels
Milestone

Comments

@DavidArno
Copy link

DavidArno commented Nov 23, 2016

Version Used: VS2017 RC

Steps to Reproduce:

class Program
{
    static void Main(string[] args)
    {
        var s = "test";
        if (s is var x)
        {
            Console.WriteLine($"x = {x}");
        }
        else
        {
            Console.WriteLine("s isn't a var!"); // unreachable code
        }
    }
}

Expected Behavior:
As s is var x will always be true, an "Unreachable code detected" warning should be reported for the contents of the else block.

Actual Behavior:
No warning is reported.

@gafter
Copy link
Member

gafter commented Nov 23, 2016

As currently specified, an unreachable code warning is only produced when the if expression is the constant true, not when it is a non-constant expression that is always true. However, this is a reasonable feature request.

@gafter
Copy link
Member

gafter commented Nov 23, 2016

@MadsTorgersen Does this belong on the LDM agenda?

@alrz
Copy link
Contributor

alrz commented Nov 28, 2016

I think there was a proposal for disallowing e is var because that's pointless! Declaration expressions would be a better solution when you want to introduce a variable in the expression context and don't care about its type.

@gafter gafter modified the milestones: 2.0 (RTM), 2.0 (RC.3) Nov 30, 2016
@gafter
Copy link
Member

gafter commented Dec 9, 2016

The LDM met yesterday and confirmed that the current behavior is intended. We do not make any reachability accommodation for an is-pattern-matching operation that is irrefutable.

@gafter gafter closed this as completed Dec 9, 2016
@gafter gafter added the Resolution-By Design The behavior reported in the issue matches the current design label Dec 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
@DavidArno @Pilchie @alrz @gafter and others