Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1513 to not trigger before an init accessor #3666

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal static class SyntaxKindEx
public const SyntaxKind OrKeyword = (SyntaxKind)8438;
public const SyntaxKind AndKeyword = (SyntaxKind)8439;
public const SyntaxKind NotKeyword = (SyntaxKind)8440;
public const SyntaxKind InitKeyword = (SyntaxKind)8443;
public const SyntaxKind ManagedKeyword = (SyntaxKind)8445;
public const SyntaxKind UnmanagedKeyword = (SyntaxKind)8446;
public const SyntaxKind RequiredKeyword = (SyntaxKind)8447;
Expand Down