Skip to content

Commit a674a3b

Browse files
committed
Document the change in behavior for SA1509
At the same time, the implementation of the new behavior was greatly simplified.
1 parent 462d9b9 commit a674a3b

3 files changed

Lines changed: 38 additions & 21 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1509OpeningBracesMustNotBePrecededByBlankLine.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,9 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
8080

8181
foreach (var openBrace in openBraces)
8282
{
83-
BlockSyntax blockSyntax = openBrace.Parent as BlockSyntax;
84-
if (blockSyntax != null)
83+
if (openBrace.GetPreviousToken().IsKind(SyntaxKind.CloseBraceToken))
8584
{
86-
BlockSyntax parentBlock = blockSyntax.Parent as BlockSyntax;
87-
if (parentBlock != null && parentBlock.Statements[0] != blockSyntax)
88-
{
89-
// Do not disallow a blank line before an independent block, unless it is the first child of its
90-
// parent.
91-
continue;
92-
}
85+
continue;
9386
}
9487

9588
AnalyzeOpenBrace(context, openBrace);

documentation/SA1509.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,43 @@ An opening brace within a C# element, statement, or expression is preceded by a
2121

2222
## Rule description
2323

24-
To improve the readability of the code, StyleCop requires blank lines in certain situations, and prohibits blank lines in other situations. This results in a consistent visual pattern across the code, which can improve recognition and readability of unfamiliar code.
24+
To improve the readability of the code, StyleCop requires blank lines in certain situations, and prohibits blank lines
25+
in other situations. This results in a consistent visual pattern across the code, which can improve recognition and
26+
readability of unfamiliar code.
2527

26-
A violation of this rule occurs when an opening brace is preceded by a blank line. For example:
28+
In general, a violation of this rule occurs when an opening brace is preceded by a blank line. For example, the
29+
following above would generate two instances of this violation, since there are two places where opening braces are
30+
preceded by blank lines.
2731

2832
```csharp
2933
public bool Enabled
3034

3135
{
32-
get
36+
get
3337

34-
{
35-
return this.enabled;
38+
{
39+
return this.enabled;
3640
}
3741
}
3842
```
3943

40-
The code above would generate two instances of this violation, since there are two places where opening braces are preceded by blank lines.
44+
An exception to this rule occurs when the opening brace is preceded by a closing brace. When this occurs, a blank line
45+
will be required by [SA1513](SA1513.md). The following example shows correct use of blank lines:
46+
47+
```csharp
48+
public bool Enabled
49+
{
50+
get
51+
{
52+
{
53+
}
54+
55+
{
56+
return this.enabled;
57+
}
58+
}
59+
}
60+
```
4161

4262
## How to fix violations
4363

documentation/SA1513.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,24 @@ A closing brace within a C# element, statement, or expression is not followed by
2121

2222
## Rule description
2323

24-
To improve the readability of the code, StyleCop requires blank lines in certain situations, and prohibits blank lines in other situations. This results in a consistent visual pattern across the code, which can improve recognition and readability of unfamiliar code.
24+
To improve the readability of the code, StyleCop requires blank lines in certain situations, and prohibits blank lines
25+
in other situations. This results in a consistent visual pattern across the code, which can improve recognition and
26+
readability of unfamiliar code.
2527

26-
A violation of this rule occurs when a closing brace is not followed by a blank line. For example:
28+
A violation of this rule occurs when a closing brace is not followed by a blank line. For example, the following code
29+
would generate one instance of this violation, since there is one place where a closing brace is not followed by a blank
30+
line.
2731

2832
```csharp
2933
public bool Enabled
3034
{
31-
get
32-
{
33-
return this.enabled;
35+
get
36+
{
37+
return this.enabled;
3438
}}
3539
```
3640

37-
The code above would generate one instance of this violation, since there is one place where a closing brace is not followed by a blank line.
41+
3842

3943
## How to fix violations
4044

0 commit comments

Comments
 (0)