Skip to content

Commit

Permalink
Merge pull request #3482 from maxkoshevoi/mk/3351-global-statement
Browse files Browse the repository at this point in the history
Fix false positive in SA1516 between global statements
  • Loading branch information
sharwell authored Apr 18, 2022
2 parents bd7c5aa + df4c6a3 commit 8f56280
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SA1516CSharp9UnitTests : SA1516CSharp8UnitTests
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3242, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3242")]
public async Task TestStatementSpacingInTopLevelProgramAsync()
public async Task TestUsingAndGlobalStatementSpacingInTopLevelProgramAsync()
{
var testCode = @"using System;
using System.Threading;
Expand Down Expand Up @@ -55,5 +55,64 @@ public async Task TestStatementSpacingInTopLevelProgramAsync()
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that SA1516 is not reported between global statement in top-level programs.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3351, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3351")]
public async Task TestGlobalStatementSpacingInTopLevelProgramAsync()
{
var testCode = @"int i = 0;
return i;
";

await new CSharpTest(LanguageVersion.CSharp9)
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources = { testCode },
},
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that SA1516 is reported between global statement and record declaration in top-level programs.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestGlobalStatementAndRecordSpacingInTopLevelProgramAsync()
{
var testCode = @"return 0;
{|#0:record|} A();
";

var fixedCode = @"return 0;
record A();
";

await new CSharpTest(LanguageVersion.CSharp9)
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources = { testCode },
ExpectedDiagnostics =
{
// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),

// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
},
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, SyntaxLi
{
for (int i = 1; i < members.Count; i++)
{
// Don't report between global statements
if (members[i - 1].IsKind(SyntaxKind.GlobalStatement) && members[i].IsKind(SyntaxKind.GlobalStatement))
{
continue;
}

if (!members[i - 1].ContainsDiagnostics && !members[i].ContainsDiagnostics)
{
// Report if
Expand Down

0 comments on commit 8f56280

Please sign in to comment.