Skip to content

Commit

Permalink
Merge pull request #3256 from NextTurn/sa1119
Browse files Browse the repository at this point in the history
Update SA1119 for stackalloc expressions
  • Loading branch information
sharwell authored Dec 1, 2020
2 parents edf811d + cbcff6b commit 8d2a0a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ public unsafe string TestMethod(int n, byte* a, byte* b)
return (n switch { 1 => a, 2 => b })->ToString();
}
}
";

await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestStackAllocExpressionInExpressionAsync()
{
const string testCode = @"public class TestClass
{
public unsafe void TestMethod()
{
var ptr1 = stackalloc byte[1];
var span1 = (stackalloc byte[1]);
var ptr2 = stackalloc[] { 0 };
var span2 = (stackalloc[] { 0 });
}
}
";

await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ private static void HandleParenthesizedExpression(SyntaxNodeAnalysisContext cont
return;
}

if ((node.Expression.IsKind(SyntaxKind.StackAllocArrayCreationExpression)
|| node.Expression.IsKind(SyntaxKindEx.ImplicitStackAllocArrayCreationExpression))
&& node.Parent.IsKind(SyntaxKind.EqualsValueClause))
{
return;
}

ReportDiagnostic(context, node);
}
else
Expand Down

0 comments on commit 8d2a0a0

Please sign in to comment.