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

Check CS0103 when try to replace type cast by 'is' pattern #55864

Merged
merged 1 commit into from
Aug 25, 2021

Conversation

Cosifne
Copy link
Member

@Cosifne Cosifne commented Aug 24, 2021

Fix #51340
In code sample

using System.Linq;
class Bar
{
    private void Foo()
    {
        var objects = new SpecificThingType[100];
        var d = from obj in objects
                let aGenericThing = obj.Prop
                where aGenericTh[||]ing is SpecificThingType
                let specificThing = (SpecificThingType)aGenericThing
                select (obj, specificThing);
    }
}
class SpecificThingType
{
    public SpecificThingType Prop { get; }
}

When we search the potential match for aGenericThing, let specificThing = (SpecificThingType)aGenericThing is included.
However, when the analyzer is trying to apply aGenericThing is SpecificThingType Baz in the WhereClause
it is unsafe to replace (SpecificThingType)aGenericThing with Baz.

So add an extra diagnostic check for CS0103

@Cosifne Cosifne merged commit 86c1bd9 into dotnet:main Aug 25, 2021
@ghost ghost added this to the Next milestone Aug 25, 2021
@Cosifne Cosifne deleted the dev/shech/IDE0038Fix branch August 25, 2021 17:11
@@ -170,7 +171,7 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext context)
var currentNode = root.GetAnnotatedNodes(s_referenceAnnotation).Single();
var diagnostics = updatedSemanticModel.GetDiagnostics(currentNode.Span, cancellationToken);

return diagnostics.Any(d => d.Id == CS0165);
return diagnostics.Any(d => d.Id == CS0165 || d.Id == CS0103);
Copy link
Member

Choose a reason for hiding this comment

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

i don't really like this approach. i think we should have just changed the analysis step to filter our matches in linq queries clauses.

@dibarbet dibarbet modified the milestones: Next, 17.0.P4 Aug 31, 2021
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.

suggestion IDE0038 + LINQ leads to error CS0103
4 participants