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 @@ -251,6 +251,23 @@ void M(int? a)
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp7_3)));
}

[Fact]
[WorkItem(36467, "https://github.com/dotnet/roslyn/issues/36467")]
[Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestNotSuggestedWhenRightHandIsThrowExpression()
{
await TestMissingAsync(
@"using System;
public class C
{
void M(int? a)
{
a [||]= a ?? throw new Exception();
}
}",
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestField()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
}

_syntaxFacts.GetPartsOfBinaryExpression(binaryExpression,
out var binaryLeft, out _);
out var binaryLeft, out var binaryRight);

// has to be of the form: expr = expr op ...
if (!_syntaxFacts.AreEquivalent(assignmentLeft, binaryLeft))
Expand All @@ -109,6 +109,12 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
return;
}

// Don't offer if this is `x = x ?? throw new Exception()`
if (_syntaxFacts.IsThrowExpression(binaryRight))
{
return;
}

// Syntactically looks promising. But we can only safely do this if 'expr'
// is side-effect-free since we will be changing the number of times it is
// executed from twice to once.
Expand Down