Skip to content

Commit 98d9cf6

Browse files
Fix for issue #2062: Updated SA1513 to fire inside e.g. lambdas in an argument list. Previously, it could never fire inside an argument list, which does not make sense if the brace was inside e.g. a lambda expression. Also updated documentation to have a correct example for this rule.
1 parent 90e043e commit 98d9cf6

3 files changed

Lines changed: 62 additions & 22 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,61 @@ public void Example()
725725
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
726726
}
727727

728+
// This is a regression test for #2062.
729+
[Fact]
730+
public async Task VerifyThatBlockStatementDirectlyFollowedByReturnInsideLambdaInsideArgumentListWillProduceDiagnosticAsync()
731+
{
732+
var testCode = @"
733+
using System;
734+
public class TestClass
735+
{
736+
public void Func1(Action action)
737+
{
738+
}
739+
740+
public void Func2(bool flag)
741+
{
742+
Func1(() =>
743+
{
744+
if (flag)
745+
{
746+
return;
747+
}
748+
return;
749+
});
750+
}
751+
}
752+
";
753+
754+
var fixedCode = @"
755+
using System;
756+
public class TestClass
757+
{
758+
public void Func1(Action action)
759+
{
760+
}
761+
762+
public void Func2(bool flag)
763+
{
764+
Func1(() =>
765+
{
766+
if (flag)
767+
{
768+
return;
769+
}
770+
771+
return;
772+
});
773+
}
774+
}
775+
";
776+
777+
var expected = this.CSharpDiagnostic().WithLocation(16, 14);
778+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
779+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
780+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
781+
}
782+
728783
/// <summary>
729784
/// Verifies the analyzer will properly handle an object initializer without assignment.
730785
/// This is a regression test for <see href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1301">DotNetAnalyzers/StyleCopAnalyzers#1301</see>.

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,6 @@ private void AnalyzeCloseBrace(SyntaxToken token)
241241
}
242242
}
243243

244-
if (IsPartOf<ArgumentListSyntax>(token))
245-
{
246-
// the close brace is part of an object initializer, anonymous function or lambda expression within an argument list.
247-
return;
248-
}
249-
250244
if (nextToken.IsKind(SyntaxKind.SemicolonToken) &&
251245
(IsPartOf<VariableDeclaratorSyntax>(token) ||
252246
IsPartOf<YieldStatementSyntax>(token) ||
@@ -260,10 +254,10 @@ private void AnalyzeCloseBrace(SyntaxToken token)
260254
return;
261255
}
262256

263-
if ((nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.CloseParenToken)) &&
264-
(IsPartOf<InitializerExpressionSyntax>(token) || IsPartOf<AnonymousObjectCreationExpressionSyntax>(token)))
257+
if (nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.CloseParenToken))
265258
{
266-
// the close brace is part of an initializer statement.
259+
// The close brace is the end of an object initializer, anonymous function, lambda expression, etc.
260+
// Comma and close parenthesis never requires a preceeding blank line.
267261
return;
268262
}
269263

@@ -291,13 +285,6 @@ private void AnalyzeCloseBrace(SyntaxToken token)
291285
return;
292286
}
293287

294-
var parenthesizedExpressionSyntax = nextToken.Parent as ParenthesizedExpressionSyntax;
295-
if (parenthesizedExpressionSyntax?.CloseParenToken == nextToken)
296-
{
297-
// the close brace is followed by the closing paren of a parenthesized expression.
298-
return;
299-
}
300-
301288
if (nextToken.IsKind(SyntaxKind.EndOfFileToken))
302289
{
303290
// this is the last close brace in the file

documentation/SA1513.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ would generate one instance of this violation, since there is one place where a
3030
line.
3131

3232
```csharp
33-
public bool Enabled
33+
if (condition)
3434
{
35-
get
36-
{
37-
return this.enabled;
38-
}}
35+
DoSomething();
36+
}
37+
return value;
3938
```
4039

4140

42-
4341
## How to fix violations
4442

4543
To fix a violation of this rule, ensure a blank line follows closing braces.

0 commit comments

Comments
 (0)