Skip to content

Commit bc21552

Browse files
authored
Merge pull request #2364 from bjornhellander/Issue2062_Sa1513NotFiredInsideLambda
Updated SA1513 to fire inside lambdas
2 parents ebf6c5b + 237bc17 commit bc21552

3 files changed

Lines changed: 62 additions & 28 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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,6 @@ private static bool StartsWithDirectiveTrivia(SyntaxTriviaList triviaList)
162162
return false;
163163
}
164164

165-
private static bool IsQueryClause(SyntaxToken token)
166-
{
167-
return (token.Parent is FromClauseSyntax) ||
168-
(token.Parent is GroupClauseSyntax);
169-
}
170-
171165
private static bool IsPartOf<T>(SyntaxToken token)
172166
{
173167
var result = false;
@@ -241,12 +235,6 @@ private void AnalyzeCloseBrace(SyntaxToken token)
241235
}
242236
}
243237

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-
250238
if (nextToken.IsKind(SyntaxKind.SemicolonToken) &&
251239
(IsPartOf<VariableDeclaratorSyntax>(token) ||
252240
IsPartOf<YieldStatementSyntax>(token) ||
@@ -260,10 +248,10 @@ private void AnalyzeCloseBrace(SyntaxToken token)
260248
return;
261249
}
262250

263-
if ((nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.CloseParenToken)) &&
264-
(IsPartOf<InitializerExpressionSyntax>(token) || IsPartOf<AnonymousObjectCreationExpressionSyntax>(token)))
251+
if (nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.CloseParenToken))
265252
{
266-
// the close brace is part of an initializer statement.
253+
// The close brace is the end of an object initializer, anonymous function, lambda expression, etc.
254+
// Comma and close parenthesis never requires a preceeding blank line.
267255
return;
268256
}
269257

@@ -291,13 +279,6 @@ private void AnalyzeCloseBrace(SyntaxToken token)
291279
return;
292280
}
293281

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-
301282
if (nextToken.IsKind(SyntaxKind.EndOfFileToken))
302283
{
303284
// 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)