Skip to content

Commit 8d2a0a0

Browse files
authored
Merge pull request #3256 from NextTurn/sa1119
Update SA1119 for stackalloc expressions
2 parents edf811d + cbcff6b commit 8d2a0a0

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ public unsafe string TestMethod(int n, byte* a, byte* b)
148148
return (n switch { 1 => a, 2 => b })->ToString();
149149
}
150150
}
151+
";
152+
153+
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
154+
}
155+
156+
[Fact]
157+
public async Task TestStackAllocExpressionInExpressionAsync()
158+
{
159+
const string testCode = @"public class TestClass
160+
{
161+
public unsafe void TestMethod()
162+
{
163+
var ptr1 = stackalloc byte[1];
164+
var span1 = (stackalloc byte[1]);
165+
var ptr2 = stackalloc[] { 0 };
166+
var span2 = (stackalloc[] { 0 });
167+
}
168+
}
151169
";
152170

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

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ private static void HandleParenthesizedExpression(SyntaxNodeAnalysisContext cont
137137
return;
138138
}
139139

140+
if ((node.Expression.IsKind(SyntaxKind.StackAllocArrayCreationExpression)
141+
|| node.Expression.IsKind(SyntaxKindEx.ImplicitStackAllocArrayCreationExpression))
142+
&& node.Parent.IsKind(SyntaxKind.EqualsValueClause))
143+
{
144+
return;
145+
}
146+
140147
ReportDiagnostic(context, node);
141148
}
142149
else

0 commit comments

Comments
 (0)