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

IDE0260 recommends switching to invalid pattern #67010

Closed
stephentoub opened this issue Feb 23, 2023 · 2 comments · Fixed by #67047
Closed

IDE0260 recommends switching to invalid pattern #67010

stephentoub opened this issue Feb 23, 2023 · 2 comments · Fixed by #67047
Labels
untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@stephentoub
Copy link
Member

Version Used:
Version 17.6.0 Preview 2.0 [33420.113.main]

Steps to Reproduce:

internal class Program
{
    static void Main() { }

    static B M(object o)
    {
        if ((o as A)?.Value is not B b)
        {
            throw new Exception();
        }

        return b;
    }
}

class A
{
    public B Value;
}

class B
{
}

Diagnostic Id:
IDE0260

Expected Behavior:
No warning.

Actual Behavior:
IDE0260 fires and recommends changing:

        if ((o as A)?.Value is not B b)

to:

        if (o is A { Value: not B b })

which is invalid and results in:

error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
@CyrusNajmabadi
Copy link
Member

Odd. The top expression defines a variable within a not pattern. @RikkiGibson do you know why the top is allowed but the bottom isn't. I keep looking at them and they seem functionally identical. It's unclear to me why the second form isn't ok.

@RikkiGibson
Copy link
Contributor

My recollection is that 'is not A a' is a special case and it's generally not allowed in other contexts. Would need to dig up the and/or/not patterns proposal to recall why.

@jaredpar jaredpar added the untriaged Issues and PRs which have not yet been triaged by a lead label Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants