Skip to content

Commit

Permalink
Merge pull request #917 from MaStr11/CC0022BugFalsePositiveWhenUsingN…
Browse files Browse the repository at this point in the history
…ullCoalescing

Fix for bug #878 and #870 CC0022 False positive when using null coalescing operator
  • Loading branch information
carloscds committed May 3, 2017
2 parents 6471990 + e74608c commit 6682f8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static void AnalyzeObjectCreation(SyntaxNodeAnalysisContext context)

var originalNode = objectCreation;
SyntaxNode topSyntaxNode = originalNode;
while (topSyntaxNode.Parent.IsAnyKind(SyntaxKind.ParenthesizedExpression, SyntaxKind.ConditionalExpression, SyntaxKind.CastExpression))
while (topSyntaxNode.Parent.IsAnyKind(SyntaxKind.ParenthesizedExpression, SyntaxKind.ConditionalExpression, SyntaxKind.CastExpression, SyntaxKind.CoalesceExpression))
topSyntaxNode = topSyntaxNode.Parent;

if (topSyntaxNode.Parent.IsAnyKind(SyntaxKind.ReturnStatement, SyntaxKind.UsingStatement, SyntaxKind.YieldReturnStatement))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,28 @@ void Bar()
var h = new HoldsDisposable();
h.Ms = m;
}
}";
await VerifyCSharpHasNoDiagnosticsAsync(source);
}

[Fact]
public async Task IgnoreWhenAssignedToFieldByNullCoalescingOperator()
{
const string source = @"
using System.IO;
public class Test : IDisposable
{
private IDisposable _stream;
public void Update()
{
_stream = _stream ?? new MemoryStream();
}
public void Dispose()
{
_stream.Dispose();
}
}";
await VerifyCSharpHasNoDiagnosticsAsync(source);
}
Expand Down

0 comments on commit 6682f8b

Please sign in to comment.