Skip to content

Commit 9edd3aa

Browse files
committed
Improved initializer handling of SA1513
1 parent 950cc79 commit 9edd3aa

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,63 @@ public void TestMethod()
739739
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
740740
}
741741

742+
/// <summary>
743+
/// Verifies that having an initializer as last parameter / argument will not raise any diagnostics.
744+
/// This is a regression for #1713
745+
/// </summary>
746+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
747+
[Fact]
748+
public async Task VerifyThatInitializerAsLastParameterWillNotProduceDiagnosticAsync()
749+
{
750+
var testCode = @"
751+
using System;
752+
753+
public class Program
754+
{
755+
private class Foo : IDisposable
756+
{
757+
public int Bar { get; set; }
758+
759+
public void Dispose()
760+
{
761+
}
762+
}
763+
764+
public static void Main(string[] args)
765+
{
766+
foreach (
767+
var x in
768+
new[]
769+
{
770+
1,
771+
2,
772+
3
773+
})
774+
{
775+
}
776+
777+
using (
778+
new Foo
779+
{
780+
Bar = 1
781+
})
782+
{
783+
}
784+
785+
Console.WriteLine(
786+
new[]
787+
{
788+
1,
789+
2,
790+
3
791+
});
792+
}
793+
}
794+
";
795+
796+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
797+
}
798+
742799
/// <inheritdoc/>
743800
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
744801
{

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingCurlyBracketMustBeFollowedByBlankLine.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ private void AnalyzeCloseBrace(SyntaxToken token)
237237
return;
238238
}
239239

240-
if (nextToken.IsKind(SyntaxKind.CommaToken) &&
241-
(IsPartOf<InitializerExpressionSyntax>(token) ||
242-
IsPartOf<AnonymousObjectCreationExpressionSyntax>(token)))
240+
if ((nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.CloseParenToken)) &&
241+
(IsPartOf<InitializerExpressionSyntax>(token) || IsPartOf<AnonymousObjectCreationExpressionSyntax>(token)))
243242
{
244243
// the close brace is part of an initializer statement.
245244
return;

0 commit comments

Comments
 (0)