Skip to content

Commit 3afc18b

Browse files
committed
Update SA1014 for function pointers
1 parent 1cb5103 commit 3afc18b

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1014CSharp9UnitTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,44 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.SpacingRules.SA1014OpeningGenericBracketsMustBeSpacedCorrectly,
12+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
713

814
public partial class SA1014CSharp9UnitTests : SA1014CSharp8UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3970, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3970")]
18+
public async Task TestFunctionPointerOpeningBracketSpacingAsync()
19+
{
20+
var testCode = @"public class TestClass
21+
{
22+
private unsafe delegate* {|#0:<|}int, void> field1;
23+
private unsafe delegate*{|#1:<|} int, void> field2;
24+
private unsafe delegate* unmanaged[Cdecl] {|#2:<|}int, void> field3;
25+
}
26+
";
27+
28+
var fixedCode = @"public class TestClass
29+
{
30+
private unsafe delegate*<int, void> field1;
31+
private unsafe delegate*<int, void> field2;
32+
private unsafe delegate* unmanaged[Cdecl]<int, void> field3;
33+
}
34+
";
35+
36+
var expected = new[]
37+
{
38+
Diagnostic().WithArguments("preceded").WithLocation(0),
39+
Diagnostic().WithArguments("followed").WithLocation(1),
40+
Diagnostic().WithArguments("preceded").WithLocation(2),
41+
};
42+
43+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
44+
}
1045
}
1146
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ internal static class SyntaxKindEx
7070
public const SyntaxKind NullableDirectiveTrivia = (SyntaxKind)9055;
7171
public const SyntaxKind FunctionPointerType = (SyntaxKind)9056;
7272
public const SyntaxKind FunctionPointerParameter = (SyntaxKind)9057;
73+
public const SyntaxKind FunctionPointerParameterList = (SyntaxKind)9058;
7374
public const SyntaxKind InitAccessorDeclaration = (SyntaxKind)9060;
7475
public const SyntaxKind WithExpression = (SyntaxKind)9061;
7576
public const SyntaxKind WithInitializerExpression = (SyntaxKind)9062;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1014OpeningGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.SpacingRules
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.Diagnostics;
1313
using StyleCop.Analyzers.Helpers;
14+
using StyleCop.Analyzers.Lightup;
1415

1516
/// <summary>
1617
/// An opening generic bracket within a C# element is not spaced correctly.
@@ -75,6 +76,7 @@ private static void HandleLessThanToken(SyntaxTreeAnalysisContext context, Synta
7576
{
7677
case SyntaxKind.TypeArgumentList:
7778
case SyntaxKind.TypeParameterList:
79+
case SyntaxKindEx.FunctionPointerParameterList:
7880
break;
7981

8082
default:

documentation/SA1014.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ A violation of this rule occurs when the spacing around an opening generic brack
2525

2626
An opening generic bracket should never be preceded or followed by whitespace, unless the bracket is the first or last character on the line.
2727

28+
The same spacing applies to the `<` that starts a C# 9 function pointer parameter list, as in `delegate*<int, void>`.
29+
2830
## How to fix violations
2931

3032
To fix a violation of this rule, ensure that there is no whitespace on either side of the opening generic bracket.

0 commit comments

Comments
 (0)