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

Add explicit cast to UseCoalesceExpressionForIfNullStatementCheckCodeFixProvider #74471

Merged
merged 6 commits into from
Aug 27, 2024

Conversation

Orachor
Copy link

@Orachor Orachor commented Jul 20, 2024

This adds an explicit cast to UseCoalesceExpressionForIfNullStatementCheckCodeFixProvider (if necessary) to make sure the code fix does not produce broken code.

Whether or not the cast is added depends on the types that are being coalesce-d

  1. Two equal types - no cast
  2. Two types that are implicitly convertible from one to another - no cast
  3. Two different types - explicit cast to final type

As for VisualBasic from what I can tell this cast is completely unnecessary, due to the coalesce expression behaving differently, however I am not an expert so please check if the tests I added are correct.

Closes #74460

@Orachor Orachor requested a review from a team as a code owner July 20, 2024 11:23
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 20, 2024
@dotnet-policy-service dotnet-policy-service bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Jul 20, 2024
@Orachor
Copy link
Author

Orachor commented Aug 25, 2024

Hi @CyrusNajmabadi think you could take a look, please? :)

Comment on lines 51 to 52
if (ShouldAddExplicitCast(syntaxFacts, semanticModel, expressionToCoalesce,
whenTrueStatement, out var castTo, cancellationToken))
Copy link
Member

Choose a reason for hiding this comment

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

i'd have this just return the castTo type as the return value. and this can then just check that for null, and cast if not.

Copy link
Author

Choose a reason for hiding this comment

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

Done


Protected Overrides Function ShouldAddExplicitCast(syntaxFacts As ISyntaxFactsService, semanticModel As SemanticModel, expressionToCoalesce As SyntaxNode, whenTrueStatement As SyntaxNode, <NotNullWhen(True)> ByRef castTo As ITypeSymbol, cancellationToken As CancellationToken) As Boolean
Return False
End Function
Copy link
Member

Choose a reason for hiding this comment

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

i'm also ok with this being virtual and returning false, so that the VB side doesn't need this.

Copy link
Member

Choose a reason for hiding this comment

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

or, if you keep this, doc why cast is not needed.

Copy link
Author

Choose a reason for hiding this comment

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

Changed method to virtual with default returning null.

@@ -33,10 +26,17 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return Task.CompletedTask;
}

protected override Task FixAllAsync(
protected abstract bool ShouldAddExplicitCast(
ISyntaxFactsService syntaxFacts, SemanticModel semanticModel,
Copy link
Member

Choose a reason for hiding this comment

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

don't need to pass syntaxfacts in. since the subclasses are already lang specific, they can directly do whatever they need.

Copy link
Author

Choose a reason for hiding this comment

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

I changed the implementation so SyntaxFacts are not passed in, now I am wondering whether I should do the same for the SemanticModel and have the language specific stuff return just true/false for whether the cast can be added?

{
castTo = null;

if (!syntaxFacts.IsSimpleAssignmentStatement(whenTrueStatement))
Copy link
Member

Choose a reason for hiding this comment

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

doc why this is only for the asignment case.

Copy link
Member

Choose a reason for hiding this comment

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

doc the basic intuition here, possible with an example.

Copy link
Author

Choose a reason for hiding this comment

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

Added a comment :)

@CyrusNajmabadi
Copy link
Member

@Orachor Great work! Love the tests. Some minor tweaks/suggestions, esp. about having hte code doc what's going on. Thanks! <3

@CyrusNajmabadi
Copy link
Member

Looks great. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IDE0270 creates invalid code
3 participants