Skip to content

Commit b38cfd7

Browse files
committed
Do not warn if a comma follows a preprocessor directive
1 parent 17b613d commit b38cfd7

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,27 @@ public void TestMethod()
349349
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
350350
}
351351

352+
[Fact]
353+
[WorkItem(3816, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3816")]
354+
public async Task TestCommaFollowingPreprocessorDirectiveAsync()
355+
{
356+
var testCode = @"
357+
interface IFormattable {}
358+
interface ISpanFormattable {}
359+
360+
partial struct Money : IFormattable
361+
#if !NETSTANDARD
362+
, ISpanFormattable
363+
#endif
364+
{
365+
}
366+
";
367+
368+
var expected = DiagnosticResult.EmptyDiagnosticResults;
369+
370+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
371+
}
372+
352373
private Task TestCommaInStatementOrDeclAsync(string originalStatement, DiagnosticResult expected, string fixedStatement)
353374
{
354375
return this.TestCommaInStatementOrDeclAsync(originalStatement, new[] { expected }, fixedStatement);

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.SpacingRules
77
{
88
using System;
99
using System.Collections.Immutable;
10+
using System.Linq;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.CSharp;
1213
using Microsoft.CodeAnalysis.Diagnostics;
@@ -76,6 +77,14 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo
7677
return;
7778
}
7879

80+
// Check if the comma follows a preprocessor directive
81+
if (token.HasLeadingTrivia && token.LeadingTrivia.Any(trivia =>
82+
trivia.IsDirective))
83+
{
84+
// Ignore this comma as it follows a preprocessor directive
85+
return;
86+
}
87+
7988
// check for a following space
8089
bool missingFollowingSpace = true;
8190

documentation/SA1001.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ A comma should be followed by a single space, except in the following cases.
2828
* A comma may appear at the end of a line
2929
* A comma should not be followed by a space when used in an open generic type in a `typeof` expression
3030
* A comma is part of a string interpolation alignment component. For example:`$"{x,3}"`
31+
* A comma follows a preprocessor directive. For example:
32+
```csharp
33+
partial struct Money : IFormattable
34+
#if !NETSTANDARD
35+
, ISpanFormattable
36+
#endif
3137

3238
A comma should never be preceded by a space or appear as the first token on a line.
3339

0 commit comments

Comments
 (0)