Skip to content

Commit 2aeb4e3

Browse files
author
Matthias Baer
committed
Do not report SA1516 for two consecutive single line properties
1 parent f5843ae commit 2aeb4e3

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1516UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class Bar
3232
public string Test3;
3333
3434
public string TestProperty1 { get; set; }
35-
3635
public string TestProperty2 { get; set; }
3736
/// <summary>
3837
/// A summary.
@@ -149,7 +148,6 @@ class Foo { }
149148
Diagnostic().WithLocation(2, 1),
150149
Diagnostic().WithLocation(5, 1),
151150
Diagnostic().WithLocation(12, 1),
152-
Diagnostic().WithLocation(13, 1),
153151
Diagnostic().WithLocation(18, 1),
154152
Diagnostic().WithLocation(24, 1),
155153
Diagnostic().WithLocation(29, 1),

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1516ElementsMustBeSeparatedByBlankLine.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,34 +345,48 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, SyntaxLi
345345

346346
if (!members[i - 1].ContainsDiagnostics && !members[i].ContainsDiagnostics)
347347
{
348-
// Report if
349-
// the current declaration is not a field declaration
350-
// or the previous declaration is of different type
351-
// or the previous declaration spans across multiple lines
352-
//
353-
// Note that the order of checking is important, as the call to IsMultiLine requires a FieldDeclarationSyntax,
354-
// something that can only be guaranteed if the first two checks fail.
355-
if (!members[i].IsKind(SyntaxKind.FieldDeclaration)
356-
|| !members[i - 1].IsKind(members[i].Kind())
357-
|| IsMultiline((FieldDeclarationSyntax)members[i - 1]))
348+
// Don't report if
349+
// the previous declaration is of the same type
350+
// AND it is either
351+
// - a single line field declaration where the previous field is single line
352+
// or
353+
// - a single line property declaration where the previous property is single line
354+
if (members[i - 1].IsKind(members[i].Kind())
355+
&& ((members[i].IsKind(SyntaxKind.FieldDeclaration)
356+
&& !IsMultiline((FieldDeclarationSyntax)members[i])
357+
&& !IsMultiline((FieldDeclarationSyntax)members[i - 1]))
358+
|| (members[i].IsKind(SyntaxKind.PropertyDeclaration)
359+
&& !IsMultiline((PropertyDeclarationSyntax)members[i])
360+
&& !IsMultiline((PropertyDeclarationSyntax)members[i - 1]))))
358361
{
359-
ReportIfThereIsNoBlankLine(context, members[i - 1], members[i]);
362+
continue;
360363
}
364+
365+
ReportIfThereIsNoBlankLine(context, members[i - 1], members[i]);
361366
}
362367
}
363368
}
364369

365370
private static bool IsMultiline(FieldDeclarationSyntax fieldDeclaration)
366371
{
367-
var lineSpan = fieldDeclaration.GetLineSpan();
368-
var attributeLists = fieldDeclaration.AttributeLists;
372+
return IsMultiline(fieldDeclaration, fieldDeclaration.AttributeLists);
373+
}
374+
375+
private static bool IsMultiline(PropertyDeclarationSyntax propertyDeclaration)
376+
{
377+
return IsMultiline(propertyDeclaration, propertyDeclaration.AttributeLists);
378+
}
379+
380+
private static bool IsMultiline(MemberDeclarationSyntax memberDeclaration, SyntaxList<AttributeListSyntax> attributeLists)
381+
{
382+
var lineSpan = memberDeclaration.GetLineSpan();
369383

370384
int startLine;
371385

372386
// Exclude attributes when determining if a field declaration spans multiple lines
373387
if (attributeLists.Count > 0)
374388
{
375-
var lastAttributeSpan = fieldDeclaration.SyntaxTree.GetLineSpan(attributeLists.Last().FullSpan);
389+
var lastAttributeSpan = memberDeclaration.SyntaxTree.GetLineSpan(attributeLists.Last().FullSpan);
376390
startLine = lastAttributeSpan.EndLinePosition.Line;
377391
}
378392
else

0 commit comments

Comments
 (0)