Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2913,4 +2913,115 @@ public void Dispose()
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81322")]
public Task TestIfStatement_CompoundAssignment_Event()
=> TestInRegularAndScriptAsync(
"""
using System;

class C
{
event Action SomeEvent;

static void M(C c)
{
[|if|] (c is not null)
c.SomeEvent += () => { };
}
}
""",
"""
using System;

class C
{
event Action SomeEvent;

static void M(C c)
{
c?.SomeEvent += () => { };
}
}
""", languageVersion: LanguageVersion.CSharp14);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81322")]
public Task TestIfStatement_CompoundAssignment_AddAssignment()
=> TestInRegularAndScriptAsync(
"""
using System;

class C
{
int Value;

static void M(C c)
{
[|if|] (c != null)
c.Value += 5;
}
}
""",
"""
using System;

class C
{
int Value;

static void M(C c)
{
c?.Value += 5;
}
}
""", languageVersion: LanguageVersion.CSharp14);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81322")]
public Task TestIfStatement_CompoundAssignment_SubtractAssignment()
=> TestInRegularAndScriptAsync(
"""
using System;

class C
{
int Value;

static void M(C c)
{
[|if|] (c != null)
c.Value -= 5;
}
}
""",
"""
using System;

class C
{
int Value;

static void M(C c)
{
c?.Value -= 5;
}
}
""", languageVersion: LanguageVersion.CSharp14);

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81322")]
public Task TestIfStatement_CompoundAssignment_NotAvailableInCSharp13()
=> TestMissingInRegularAndScriptAsync(
"""
using System;

class C
{
event Action SomeEvent;

static void M(C c)
{
if (c is not null)
c.SomeEvent += () => { };
}
}
""", LanguageVersion.CSharp13);
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
if (!AnalyzeLocalDeclarationForm(previousStatement, out expressionToCoalesce))
return;
}
else if (syntaxFacts.IsAnyAssignmentStatement(previousStatement))
else if (syntaxFacts.IsSimpleAssignmentStatement(previousStatement))
{
// v = Expr();
// if (v == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ private static TExpressionSyntax RemoveObjectCastIfAny(
if (node is TElementAccessExpressionSyntax elementAccess)
return (TExpressionSyntax?)syntaxFacts.GetExpressionOfElementAccessExpression(elementAccess);

if (syntaxFacts.SyntaxKinds.SimpleAssignmentExpression == node.RawKind && syntaxFacts.SupportsNullConditionalAssignment(node.SyntaxTree.Options))
if (syntaxFacts.IsAnyAssignmentStatement(node.Parent) &&
syntaxFacts.SupportsNullConditionalAssignment(node.SyntaxTree.Options))
{
syntaxFacts.GetPartsOfAssignmentExpressionOrStatement(node, out var left, out _, out _);
return (TExpressionSyntax)left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void AnalyzeIfStatementAndReportDiagnostic(
// `if (<expr> != null) { <expr>.Method(); <expr> = null; }`
//
// If 'expr' is not null, then we execute the body and then end up with expr being null. So `expr?.Method(); expr = null;`
// preserves those semantics. Simialrly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it
// preserves those semantics. Simialarly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it
Copy link
Contributor

Choose a reason for hiding this comment

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

I love how copilot found this typo and... made it worse 😂

// the same as well. So this is a valid conversion in all cases.
if (!syntaxFacts.IsSimpleAssignmentStatement(nullAssignmentOpt))
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,9 +1203,11 @@ public bool IsDeclaration(SyntaxNode? node)
public bool IsTypeDeclaration(SyntaxNode node)
=> SyntaxFacts.IsTypeDeclaration(node.Kind());

public bool IsSimpleAssignmentStatement([NotNullWhen(true)] SyntaxNode? statement)
=> statement is ExpressionStatementSyntax exprStatement &&
exprStatement.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression);
public bool IsSimpleAssignmentStatement([NotNullWhen(true)] SyntaxNode? node)
=> node is ExpressionStatementSyntax { Expression: (kind: SyntaxKind.SimpleAssignmentExpression) };

public bool IsAnyAssignmentStatement([NotNullWhen(true)] SyntaxNode? node)
=> node is ExpressionStatementSyntax { Expression: AssignmentExpressionSyntax };

public void GetPartsOfAssignmentStatement(
SyntaxNode statement, out SyntaxNode left, out SyntaxToken operatorToken, out SyntaxNode right)
Expand All @@ -1229,10 +1231,6 @@ public void GetPartsOfAssignmentExpressionOrStatement(
right = assignment.Right;
}

// C# does not have assignment statements.
public bool IsAnyAssignmentStatement([NotNullWhen(true)] SyntaxNode? node)
=> false;

public SyntaxToken GetIdentifierOfSimpleName(SyntaxNode node)
=> ((SimpleNameSyntax)node).Identifier;

Expand Down
Loading