diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs index d5627e660..00625e005 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs @@ -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); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs index 796b76e23..3253778d4 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs @@ -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