Skip to content

Commit

Permalink
Merge pull request #3254 from NextTurn/sa1011
Browse files Browse the repository at this point in the history
Fix SA1011 for function pointer unmanaged calling convention list
  • Loading branch information
sharwell authored Nov 30, 2020
2 parents 7ae1443 + da6bc8a commit 78eff0d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,40 @@

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

public class SA1011CSharp9UnitTests : SA1011CSharp8UnitTests
{
[Fact]
public async Task TestFunctionPointerUnmanagedCallingConventionListAsync()
{
var testCode = @"public class TestClass
{
unsafe delegate* unmanaged[Stdcall {|#0:]|}<void> FuncPtr1;
unsafe delegate* unmanaged[Stdcall{|#1:]|} <void> FuncPtr2;
}
";

var fixedCode = @"public class TestClass
{
unsafe delegate* unmanaged[Stdcall]<void> FuncPtr1;
unsafe delegate* unmanaged[Stdcall]<void> FuncPtr2;
}
";

var expected = new[]
{
Diagnostic().WithArguments(" not", "preceded").WithLocation(0),
Diagnostic().WithArguments(" not", "followed").WithLocation(1),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ internal static class SyntaxKindEx
public const SyntaxKind NullableDirectiveTrivia = (SyntaxKind)9055;
public const SyntaxKind WithInitializerExpression = (SyntaxKind)9062;
public const SyntaxKind RecordDeclaration = (SyntaxKind)9063;
public const SyntaxKind FunctionPointerUnmanagedCallingConventionList = (SyntaxKind)9066;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// A closing square bracket within a C# statement is not spaced correctly.
Expand Down Expand Up @@ -114,6 +115,11 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
suppressFollowingSpaceError = false;
break;

case SyntaxKind.LessThanToken:
precedesSpecialCharacter = token.Parent.IsKind(SyntaxKindEx.FunctionPointerUnmanagedCallingConventionList);
suppressFollowingSpaceError = false;
break;

case SyntaxKind.GreaterThanToken:
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.TypeArgumentList);
break;
Expand Down

0 comments on commit 78eff0d

Please sign in to comment.