Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive in SA1516 between global statements #3482

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Comment on lines +107 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ Do we know why this is reported twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also reported twice in the first test in this file (from which I copied this one), so it's an existing behavior.
Not sure why it's reposted twice (or even is it actually reported twice, or it's a test system issue)

},
},
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