Skip to content

Commit 81ec89a

Browse files
Update SA1023 to not trigger first in line, inside a foreach without braces
#3538
1 parent 6478404 commit 81ec89a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1023UnitTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ unsafe void TestMethod()
5252
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5353
}
5454

55+
/// <summary>
56+
/// Verifies that the analyzer will properly handle valid dereference.
57+
/// </summary>
58+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
59+
[Fact]
60+
[WorkItem(3538, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3538")]
61+
public async Task TestNotReportedWhenFirstInForeachWithoutBracesAsync()
62+
{
63+
var testCode = @"
64+
public unsafe class TestClass
65+
{
66+
internal void TestMethod(Obj[] objs)
67+
{
68+
foreach (var o in objs)
69+
*o.I = 1;
70+
}
71+
72+
internal struct Obj
73+
{
74+
public int* I;
75+
}
76+
}
77+
";
78+
79+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
80+
}
81+
5582
/// <summary>
5683
/// Verifies that the analyzer will properly handle invalid dereference and access of symbols.
5784
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1023DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ private static void HandleAsteriskToken(SyntaxTreeAnalysisContext context, Synta
198198
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotAtBeginningOfLine, token.GetLocation(), properties));
199199
#pragma warning restore RS1005 // ReportDiagnostic invoked with an unsupported DiagnosticDescriptor
200200
}
201+
else if (allowAtLineStart && firstInLine)
202+
{
203+
// The case below should not trigger
204+
}
201205
else if (!allowPrecedingSpace && precededBySpace)
202206
{
203207
// Dereference symbol '*' should {not be preceded by a space}.

0 commit comments

Comments
 (0)