Skip to content

Commit 64d88c5

Browse files
Merge pull request #6 from bjornhellander/feature/sa1515-expression-bodied-members
Update SA1515SingleLineCommentMustBePrecededByBlankLine to not require a blank line at the start of an expression body
2 parents 82f80b1 + acddd2c commit 64d88c5

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1515CSharp7UnitTests.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,83 @@ public void Method()
3636

3737
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3838
}
39+
40+
/// <summary>
41+
/// Verifies that the analyzer does not fire in expression bodied property accessors.
42+
/// </summary>
43+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
44+
[Fact]
45+
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
46+
public async Task TestExpressionBodiedPropertyAccessorsAsync()
47+
{
48+
var testCode = @"
49+
class TestClass
50+
{
51+
public int TestProperty
52+
{
53+
get =>
54+
// A comment line
55+
42;
56+
57+
set =>
58+
// A comment line
59+
_ = value;
60+
}
61+
}";
62+
63+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
64+
}
65+
66+
/// <summary>
67+
/// Verifies that the analyzer does not fire in expression bodied indexer accessors.
68+
/// </summary>
69+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
70+
[Fact]
71+
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
72+
public async Task TestExpressionBodiedIndexerAccessorsAsync()
73+
{
74+
var testCode = @"
75+
class TestClass
76+
{
77+
public int this[int i]
78+
{
79+
get =>
80+
// A comment line
81+
42;
82+
83+
set =>
84+
// A comment line
85+
_ = value;
86+
}
87+
}";
88+
89+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
90+
}
91+
92+
/// <summary>
93+
/// Verifies that the analyzer does not fire in expression bodied event accessors.
94+
/// </summary>
95+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
96+
[Fact]
97+
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
98+
public async Task TestExpressionBodiedEventAccessorsAsync()
99+
{
100+
var testCode = @"
101+
class TestClass
102+
{
103+
public event System.Action TestEvent
104+
{
105+
add =>
106+
// A comment line
107+
_ = value;
108+
109+
remove =>
110+
// A comment line
111+
_ = value;
112+
}
113+
}";
114+
115+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
116+
}
39117
}
40118
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1515UnitTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,62 @@ class TestClass
301301

302302
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
303303
}
304+
305+
/// <summary>
306+
/// Verifies that the analyzer does not fire in expression bodied properties.
307+
/// </summary>
308+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
309+
[Fact]
310+
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
311+
public async Task TestExpressionBodiedPropertyAsync()
312+
{
313+
var testCode = @"
314+
class TestClass
315+
{
316+
public int TestProperty =>
317+
// A comment line
318+
42;
319+
}";
320+
321+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
322+
}
323+
324+
/// <summary>
325+
/// Verifies that the analyzer does not fire in expression bodied indexers.
326+
/// </summary>
327+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
328+
[Fact]
329+
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
330+
public async Task TestExpressionBodiedIndexerAsync()
331+
{
332+
var testCode = @"
333+
class TestClass
334+
{
335+
public int this[int i] =>
336+
// A comment line
337+
42;
338+
}";
339+
340+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
341+
}
342+
343+
/// <summary>
344+
/// Verifies that the analyzer does not fire in expression bodied methods.
345+
/// </summary>
346+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
347+
[Fact]
348+
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
349+
public async Task TestExpressionBodiedMethodAsync()
350+
{
351+
var testCode = @"
352+
class TestClass
353+
{
354+
public int TestMethod(int x) =>
355+
// A comment line
356+
42;
357+
}";
358+
359+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
360+
}
304361
}
305362
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1515SingleLineCommentMustBePrecededByBlankLine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ private static bool IsAtStartOfScope(SyntaxTrivia trivia)
269269
|| (prevToken.IsKind(SyntaxKind.OpenBracketToken) && prevToken.Parent.IsKind(SyntaxKindEx.CollectionExpression))
270270
|| prevToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
271271
|| prevToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel)
272-
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel);
272+
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel)
273+
|| prevToken.IsKind(SyntaxKind.EqualsGreaterThanToken);
273274
}
274275

275276
private static bool IsPrecededByDirectiveTrivia<T>(T triviaList, int triviaIndex)

0 commit comments

Comments
 (0)