Skip to content

Commit e40ab1f

Browse files
Update SA1206 to handle c# 11 modifier "required"
#3527
1 parent 84e2324 commit e40ab1f

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/OrderingRules/SA1206CSharp11CodeFixProviderUnitTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.CSharp11.OrderingRules
66
using System.Threading;
77
using System.Threading.Tasks;
88
using StyleCop.Analyzers.Test.CSharp10.OrderingRules;
9+
using StyleCop.Analyzers.Test.Verifiers;
910
using Xunit;
1011
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1112
StyleCop.Analyzers.OrderingRules.SA1206DeclarationKeywordsMustFollowOrder,
@@ -23,5 +24,36 @@ public async Task VerifyFileKeywordReorderingInClassDeclarationAsync()
2324
var expected = Diagnostic().WithLocation(0).WithArguments("file", "unsafe");
2425
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
2526
}
27+
28+
[Fact]
29+
[WorkItem(3527, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3527")]
30+
public async Task VerifyRequiredKeywordReorderingInPropertiesAndFieldsAsync()
31+
{
32+
var testCode = @"
33+
internal struct SomeStruct
34+
{
35+
required {|#0:public|} int Prop { get; set; }
36+
required {|#1:public|} int Field;
37+
}";
38+
39+
var fixedCode = @"
40+
internal struct SomeStruct
41+
{
42+
public required int Prop { get; set; }
43+
public required int Field;
44+
}";
45+
46+
await new CSharpTest()
47+
{
48+
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet70,
49+
TestCode = testCode,
50+
FixedCode = fixedCode,
51+
ExpectedDiagnostics =
52+
{
53+
Diagnostic().WithLocation(0).WithArguments("public", "required"),
54+
Diagnostic().WithLocation(1).WithArguments("public", "required"),
55+
},
56+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
57+
}
2658
}
2759
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/GenericAnalyzerTest.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace StyleCop.Analyzers.Test.Verifiers
77
{
88
using System;
99
using System.Collections.Immutable;
10-
using System.IO;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using Microsoft.CodeAnalysis;
@@ -23,6 +22,8 @@ internal static class GenericAnalyzerTest
2322

2423
internal static readonly ReferenceAssemblies ReferenceAssembliesNet60;
2524

25+
internal static readonly ReferenceAssemblies ReferenceAssembliesNet70;
26+
2627
private static readonly Lazy<IExportProviderFactory> ExportProviderFactory;
2728

2829
static GenericAnalyzerTest()
@@ -44,15 +45,11 @@ static GenericAnalyzerTest()
4445
ReferenceAssembliesNet50 = ReferenceAssemblies.Net.Net50.AddPackages(ImmutableArray.Create(
4546
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
4647

47-
ReferenceAssembliesNet60 =
48-
new ReferenceAssemblies(
49-
"net6.0",
50-
new PackageIdentity(
51-
"Microsoft.NETCore.App.Ref",
52-
"6.0.0"),
53-
Path.Combine("ref", "net6.0"))
54-
.AddPackages(ImmutableArray.Create(
55-
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
48+
ReferenceAssembliesNet60 = ReferenceAssemblies.Net.Net60.AddPackages(ImmutableArray.Create(
49+
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
50+
51+
ReferenceAssembliesNet70 = ReferenceAssemblies.Net.Net70.AddPackages(ImmutableArray.Create(
52+
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion)));
5653

5754
ExportProviderFactory = new Lazy<IExportProviderFactory>(
5855
() =>

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/ModifierOrderHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ internal static ModifierType GetModifierType(SyntaxToken modifier)
6969
case SyntaxKind.AsyncKeyword:
7070
case SyntaxKind.PartialKeyword:
7171
case SyntaxKind.RefKeyword:
72+
case SyntaxKindEx.RequiredKeyword:
7273
result = ModifierType.Other;
7374
break;
7475
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal static class SyntaxKindEx
1414
public const SyntaxKind NotKeyword = (SyntaxKind)8440;
1515
public const SyntaxKind ManagedKeyword = (SyntaxKind)8445;
1616
public const SyntaxKind UnmanagedKeyword = (SyntaxKind)8446;
17+
public const SyntaxKind RequiredKeyword = (SyntaxKind)8447;
1718
public const SyntaxKind FileKeyword = (SyntaxKind)8449;
1819
public const SyntaxKind NullableKeyword = (SyntaxKind)8486;
1920
public const SyntaxKind EnableKeyword = (SyntaxKind)8487;

0 commit comments

Comments
 (0)