Skip to content

Commit 8f56280

Browse files
authored
Merge pull request #3482 from maxkoshevoi/mk/3351-global-statement
Fix false positive in SA1516 between global statements
2 parents bd7c5aa + df4c6a3 commit 8f56280

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1516CSharp9UnitTests.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class SA1516CSharp9UnitTests : SA1516CSharp8UnitTests
2424
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
2525
[Fact]
2626
[WorkItem(3242, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3242")]
27-
public async Task TestStatementSpacingInTopLevelProgramAsync()
27+
public async Task TestUsingAndGlobalStatementSpacingInTopLevelProgramAsync()
2828
{
2929
var testCode = @"using System;
3030
using System.Threading;
@@ -55,5 +55,64 @@ public async Task TestStatementSpacingInTopLevelProgramAsync()
5555
FixedCode = fixedCode,
5656
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
5757
}
58+
59+
/// <summary>
60+
/// Verifies that SA1516 is not reported between global statement in top-level programs.
61+
/// </summary>
62+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
63+
[Fact]
64+
[WorkItem(3351, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3351")]
65+
public async Task TestGlobalStatementSpacingInTopLevelProgramAsync()
66+
{
67+
var testCode = @"int i = 0;
68+
return i;
69+
";
70+
71+
await new CSharpTest(LanguageVersion.CSharp9)
72+
{
73+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
74+
TestState =
75+
{
76+
OutputKind = OutputKind.ConsoleApplication,
77+
Sources = { testCode },
78+
},
79+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
80+
}
81+
82+
/// <summary>
83+
/// Verifies that SA1516 is reported between global statement and record declaration in top-level programs.
84+
/// </summary>
85+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
86+
[Fact]
87+
public async Task TestGlobalStatementAndRecordSpacingInTopLevelProgramAsync()
88+
{
89+
var testCode = @"return 0;
90+
{|#0:record|} A();
91+
";
92+
93+
var fixedCode = @"return 0;
94+
95+
record A();
96+
";
97+
98+
await new CSharpTest(LanguageVersion.CSharp9)
99+
{
100+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
101+
TestState =
102+
{
103+
OutputKind = OutputKind.ConsoleApplication,
104+
Sources = { testCode },
105+
ExpectedDiagnostics =
106+
{
107+
// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
108+
Diagnostic().WithLocation(0),
109+
110+
// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
111+
Diagnostic().WithLocation(0),
112+
},
113+
},
114+
FixedCode = fixedCode,
115+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
116+
}
58117
}
59118
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1516ElementsMustBeSeparatedByBlankLine.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, SyntaxLi
295295
{
296296
for (int i = 1; i < members.Count; i++)
297297
{
298+
// Don't report between global statements
299+
if (members[i - 1].IsKind(SyntaxKind.GlobalStatement) && members[i].IsKind(SyntaxKind.GlobalStatement))
300+
{
301+
continue;
302+
}
303+
298304
if (!members[i - 1].ContainsDiagnostics && !members[i].ContainsDiagnostics)
299305
{
300306
// Report if

0 commit comments

Comments
 (0)