Skip to content

Don't offer to use patterns when they won't work#60370

Merged
davidwengier merged 6 commits intodotnet:mainfrom
davidwengier:UsePattern
Mar 28, 2022
Merged

Don't offer to use patterns when they won't work#60370
davidwengier merged 6 commits intodotnet:mainfrom
davidwengier:UsePattern

Conversation

@davidwengier
Copy link
Copy Markdown
Member

Fixes #57199

@davidwengier davidwengier marked this pull request as ready for review March 25, 2022 05:47
@davidwengier davidwengier requested a review from a team as a code owner March 25, 2022 05:47
Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using SpeculativeSemanticModel better for this scenario? That way we'll be referring to the compiler back instead of trying to mimmic its logic.

I believe @CyrusNajmabadi has used speculation for the unnecessary cast feature, and it worked reallyy great.

Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have another idea. Why not keep the analyzer the same, ie, report a diagnostic, and fix the CodeFixProvider to generate a valid code. It's always possible to refer to the type using a fully qualified name with global:: I think?

We can always produce the node with global:: + fully qualified name, then add WithAdditionalAnnotations(Simplifier.Annotation); which should simplify the name if possible.

@davidwengier
Copy link
Copy Markdown
Member Author

Might just be me, but I wouldn't like a fix that uses global::, so I wouldn't want anything offered unless we can be sure it would be simplified out.

I'm not sure this warrants the complexity.

Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work for the following?

public class Goo
{
    private struct X { }
    private struct Y { }
    private void M(object o)
    {
        const int X = 1;
        const int Y = 2;
        // Generics are also to be considered.
        if (o is X? || o is Y?) // error if converted to o is X? or Y?
        {
        }
    }
}

I think there can be other edge cases we cannot see easily (the above may not be included, it could be currently working). That's why I think referring back to SpeculativeSemanticModel is the most accurate fix.

Comment on lines +44 to +48
var names = operation.SemanticModel.LookupSymbols(typeSyntax.SpanStart, name: GetLeftmostName(typeSyntax));
if (names.Any(t => t is not INamespaceOrTypeSymbol))
{
return null;
}
Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge case:

        var @int = 1;
        var @long = 2;
        if (o is int || o is long)
        {
        }

I think we will not produce a diagnostic while it's valid to convert this to or?

NOTE: The edge cases I'm trying to come up with doesn't mean I feel strongly they should be handled. I'm just pointing to them as they come to mind so that you can weigh on whether they should be handled or not, or add tests for them if they already work and I'm thinking they don't work.

@davidwengier
Copy link
Copy Markdown
Member Author

That's why I think referring back to SpeculativeSemanticModel is the most accurate fix.

I did try using that, but it didn't work. I've never used it before though so could very well be something I did wrong.

@Youssef1313
Copy link
Copy Markdown
Member

@davidwengier This has worked for me:

3a708f6 (built on top of your branch)

The added test was failing before 3a708f6.

@davidwengier
Copy link
Copy Markdown
Member Author

Thank you for the education @Youssef1313, I was way off!!

Copy link
Copy Markdown

@mychang666 mychang666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`

@davidwengier
Copy link
Copy Markdown
Member Author

@mychang666 Did you accidentally submit your comment early?

@davidwengier
Copy link
Copy Markdown
Member Author

Ping @dotnet/roslyn-ide and/or @CyrusNajmabadi for review

{
return null;
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cute!

@davidwengier
Copy link
Copy Markdown
Member Author

Thanks for your help @Youssef1313

@davidwengier davidwengier merged commit dc76348 into dotnet:main Mar 28, 2022
@davidwengier davidwengier deleted the UsePattern branch March 28, 2022 22:41
@ghost ghost added this to the Next milestone Mar 28, 2022
@allisonchou allisonchou modified the milestones: Next, 17.2.P3 Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive in IDE0078 (use of pattern matching)

5 participants