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 SA1023 for C# 9 function pointer parameters #3252

Merged
merged 4 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,9 +3,66 @@

namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
using Xunit;
using static StyleCop.Analyzers.SpacingRules.SA1023DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1023DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public class SA1023CSharp9UnitTests : SA1023CSharp8UnitTests
{
[Fact]
public async Task TestFunctionPointerParameterValidSpacingAsync()
{
var testCode = @"public class TestClass
{
unsafe delegate*<int*> FuncPtr;
nxtn marked this conversation as resolved.
Show resolved Hide resolved
nxtn marked this conversation as resolved.
Show resolved Hide resolved
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestFunctionPointerParameterInvalidPrecedingSpaceAsync()
{
var testCode = @"public class TestClass
{
unsafe delegate*<int *> FuncPtr;
}
";

var fixedCode = @"public class TestClass
{
unsafe delegate*<int*> FuncPtr;
}
";

var expected = Diagnostic(DescriptorNotPreceded).WithLocation(3, 26);
nxtn marked this conversation as resolved.
Show resolved Hide resolved
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestFunctionPointerParameterInvalidTrailingSpaceAsync()
{
var testCode = @"public class TestClass
{
unsafe delegate*<int* > FuncPtr;
}
";

var fixedCode = @"public class TestClass
{
unsafe delegate*<int*> FuncPtr;
}
";

var expected = Diagnostic(DescriptorNotFollowed).WithLocation(3, 25);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ internal static class SyntaxKindEx
public const SyntaxKind ImplicitStackAllocArrayCreationExpression = (SyntaxKind)9053;
public const SyntaxKind SuppressNullableWarningExpression = (SyntaxKind)9054;
public const SyntaxKind NullableDirectiveTrivia = (SyntaxKind)9055;
public const SyntaxKind FunctionPointerParameter = (SyntaxKind)9057;
public const SyntaxKind WithInitializerExpression = (SyntaxKind)9062;
public const SyntaxKind RecordDeclaration = (SyntaxKind)9063;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// A dereference symbol or an access-of symbol within a C# element is not spaced correctly.
Expand Down Expand Up @@ -113,6 +114,13 @@ private static void HandleAsteriskToken(SyntaxTreeAnalysisContext context, Synta
bool allowTrailingSpace;
switch (token.Parent.Kind())
{
case SyntaxKind.PointerType when token.Parent.Parent.IsKind(SyntaxKindEx.FunctionPointerParameter):
allowAtLineStart = true;
allowAtLineEnd = true;
allowPrecedingSpace = false;
allowTrailingSpace = false;
break;

case SyntaxKind.PointerType:
allowAtLineStart = false;
allowAtLineEnd = true;
Expand Down