Skip to content

Commit

Permalink
Merge pull request #3666 from bjornhellander/feature/sa1513-init-acce…
Browse files Browse the repository at this point in the history
…ssor

Update SA1513 to not trigger before an init accessor
  • Loading branch information
sharwell authored Jun 21, 2023
2 parents c991261 + 929986b commit 7501f04
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,33 @@ public void Baz(string arg)

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3658, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3658")]
public async Task TestInitAccessorAsync()
{
var testCode = @"using System;
public class Foo
{
public int X
{
get
{
return 0;
}
init
{
}
}
}
";

await new CSharpTest
{
TestCode = testCode,
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,53 @@ record A();
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3658, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3658")]
public async Task TestInitAccessorAsync()
{
var testCode = @"using System;
public class Foo
{
public int X
{
get
{
return 0;
}
[| |]init
{
}
}
}
";

var fixedCode = @"using System;
public class Foo
{
public int X
{
get
{
return 0;
}
init
{
}
}
}
";

await new CSharpTest
{
TestCode = testCode,
FixedCode = fixedCode,
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

protected virtual DiagnosticResult[] GetExpectedResultTestUsingAndGlobalStatementSpacingInTopLevelProgram()
{
// NOTE: Seems like a Roslyn bug made diagnostics be reported twice. Fixed in a later version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace StyleCop.Analyzers.LayoutRules
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// A closing brace within a C# element, statement, or expression is not followed by a blank line.
Expand Down Expand Up @@ -274,7 +275,8 @@ private void AnalyzeCloseBrace(SyntaxToken token)
if (nextToken.IsKind(SyntaxKind.AddKeyword)
|| nextToken.IsKind(SyntaxKind.RemoveKeyword)
|| nextToken.IsKind(SyntaxKind.GetKeyword)
|| nextToken.IsKind(SyntaxKind.SetKeyword))
|| nextToken.IsKind(SyntaxKind.SetKeyword)
|| nextToken.IsKind(SyntaxKindEx.InitKeyword))
{
// the close brace is followed by an accessor (SA1516 will handle that)
return;
Expand Down

0 comments on commit 7501f04

Please sign in to comment.