Skip to content

Commit

Permalink
Update SA1513 to not trigger before an init accessor. Also add SA1516…
Browse files Browse the repository at this point in the history
… test to verify that it already handles init accessors correctly.

#3658
  • Loading branch information
bjornhellander committed Jun 8, 2023
1 parent b80f0c2 commit 17b8bd6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1513ClosingBraceMustBeFollowedByBlankLine,
Expand Down Expand Up @@ -37,5 +38,34 @@ 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
{
}
}
}
";

var test = new CSharpTest
{
TestCode = testCode,
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
};
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1516ElementsMustBeSeparatedByBlankLine,
Expand Down Expand Up @@ -104,6 +105,54 @@ 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
{
}
}
}
";

var test = new CSharpTest
{
TestCode = testCode,
FixedCode = fixedCode,
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
};
await test.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

0 comments on commit 17b8bd6

Please sign in to comment.