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

Enhance SyntaxLogicalInverter to handle CoalesceExpressions and add corresponding tests #1069

Merged

Conversation

jamesHargreaves12
Copy link
Contributor

Add explicit handling of CoalesceExpressions in the SyntaxLogicalInverter class. Currently, in Debug mode the Debug.Fail on L213 will be evaluated which is a less-than-ideal experience.

This PR also adds intelligent support for the case where the RHS of the coalesce operator is either true or false.

  • x??true is inverted to x==false
  • x??false is inverted to x!=true

case SyntaxKind.CoalesceExpression:
{
var binaryExpression = (BinaryExpressionSyntax)expression;
if (binaryExpression.Right.Kind() == SyntaxKind.FalseLiteralExpression)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use IsKind instead of Kind. It is preferred in the codebase. It has also slightly better perf.

if (binaryExpression.Right.Kind() == SyntaxKind.FalseLiteralExpression)
{
// !(x ?? false) === (x != true)
return BinaryExpression(SyntaxKind.NotEqualsExpression, binaryExpression.Left, LiteralExpression(SyntaxKind.TrueLiteralExpression));
Copy link
Collaborator

Choose a reason for hiding this comment

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

CSharpFactory provides some helper methods to make these calls shorter: CSharpFactory.NotEqualsExpression and CSharpFactory.TrueLiteralExpression.

@josefpihrt josefpihrt merged commit e017499 into dotnet:main Apr 15, 2023
@josefpihrt
Copy link
Collaborator

👍

Haarmees pushed a commit to Haarmees/Roslynator that referenced this pull request Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants