Skip to content

Commit b07e89b

Browse files
authored
Merge pull request #36494 from YairHalberstadt/use-compound-assignment-not-suggested-when-right-hand-is-throw-expression
Do not suggest or diagnose use compound assignment when right hand of…
2 parents cad93d5 + 4b14b15 commit b07e89b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/EditorFeatures/CSharpTest/UseCompoundAssignment/UseCompoundAssignmentTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ void M(int? a)
251251
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp7_3)));
252252
}
253253

254+
[Fact]
255+
[WorkItem(36467, "https://github.com/dotnet/roslyn/issues/36467")]
256+
[Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
257+
public async Task TestNotSuggestedWhenRightHandIsThrowExpression()
258+
{
259+
await TestMissingAsync(
260+
@"using System;
261+
public class C
262+
{
263+
void M(int? a)
264+
{
265+
a [||]= a ?? throw new Exception();
266+
}
267+
}",
268+
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8)));
269+
}
270+
254271
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
255272
public async Task TestField()
256273
{

src/Features/Core/Portable/UseCompoundAssignment/AbstractUseCompoundAssignmentDiagnosticAnalyzer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
9494
}
9595

9696
_syntaxFacts.GetPartsOfBinaryExpression(binaryExpression,
97-
out var binaryLeft, out _);
97+
out var binaryLeft, out var binaryRight);
9898

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

112+
// Don't offer if this is `x = x ?? throw new Exception()`
113+
if (_syntaxFacts.IsThrowExpression(binaryRight))
114+
{
115+
return;
116+
}
117+
112118
// Syntactically looks promising. But we can only safely do this if 'expr'
113119
// is side-effect-free since we will be changing the number of times it is
114120
// executed from twice to once.

0 commit comments

Comments
 (0)