Skip to content

Commit eceae28

Browse files
committed
Fix SA1205 support for 'private protected'
1 parent 139c3c7 commit eceae28

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal class SA1205CodeFixProvider : CodeFixProvider
2626
private static readonly ImmutableArray<SyntaxKind> InternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.InternalKeyword);
2727
private static readonly ImmutableArray<SyntaxKind> ProtectedAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.ProtectedKeyword);
2828
private static readonly ImmutableArray<SyntaxKind> ProtectedOrInternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.ProtectedKeyword, SyntaxKind.InternalKeyword);
29+
private static readonly ImmutableArray<SyntaxKind> ProtectedAndInternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.PrivateKeyword, SyntaxKind.ProtectedKeyword);
2930
private static readonly ImmutableArray<SyntaxKind> PrivateAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.PrivateKeyword);
3031
private static readonly ImmutableArray<SyntaxKind> UnexpectedAccessibilityKeywords = ImmutableArray.Create<SyntaxKind>();
3132

@@ -89,6 +90,8 @@ private static ImmutableArray<SyntaxKind> GetMissingAccessModifiers(Accessibilit
8990
return ProtectedAccessibilityKeywords;
9091
case Accessibility.ProtectedOrInternal:
9192
return ProtectedOrInternalAccessibilityKeywords;
93+
case Accessibility.ProtectedAndInternal:
94+
return ProtectedAndInternalAccessibilityKeywords;
9295
case Accessibility.Private:
9396
return PrivateAccessibilityKeywords;
9497
default:

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1205UnitTests.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis.CSharp;
910
using Microsoft.CodeAnalysis.Testing;
1011
using StyleCop.Analyzers.Lightup;
1112
using StyleCop.Analyzers.OrderingRules;
@@ -96,13 +97,21 @@ public static IEnumerable<object[]> ValidNestedDeclarations
9697
yield return new object[] { "protected internal", "interface" };
9798
yield return new object[] { "private", "interface" };
9899

100+
if (LightupHelpers.SupportsCSharp72)
101+
{
102+
yield return new object[] { "private protected", "class" };
103+
yield return new object[] { "private protected", "struct" };
104+
yield return new object[] { "private protected", "interface" };
105+
}
106+
99107
if (LightupHelpers.SupportsCSharp9)
100108
{
101109
yield return new object[] { "public", "record" };
102110
yield return new object[] { "protected", "record" };
103111
yield return new object[] { "internal", "record" };
104112
yield return new object[] { "protected internal", "record" };
105113
yield return new object[] { "private", "record" };
114+
yield return new object[] { "private protected", "record" };
106115
}
107116
}
108117
}
@@ -225,7 +234,14 @@ internal static partial class TestPartial
225234
}}
226235
";
227236

228-
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
237+
var languageVersion = (LightupHelpers.SupportsCSharp8, LightupHelpers.SupportsCSharp72) switch
238+
{
239+
// Make sure to use C# 7.2 if supported, unless we are going to default to something greater
240+
(false, true) => LanguageVersionEx.CSharp7_2,
241+
_ => (LanguageVersion?)null,
242+
};
243+
244+
await VerifyCSharpDiagnosticAsync(languageVersion, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
229245
}
230246

231247
/// <summary>
@@ -294,7 +310,14 @@ public class Foo
294310
}}
295311
";
296312

297-
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(8, 14 + typeKeyword.Length), fixedTestCode, CancellationToken.None).ConfigureAwait(false);
313+
var languageVersion = (LightupHelpers.SupportsCSharp8, LightupHelpers.SupportsCSharp72) switch
314+
{
315+
// Make sure to use C# 7.2 if supported, unless we are going to default to something greater
316+
(false, true) => LanguageVersionEx.CSharp7_2,
317+
_ => (LanguageVersion?)null,
318+
};
319+
320+
await VerifyCSharpFixAsync(languageVersion, testCode, Diagnostic().WithLocation(8, 14 + typeKeyword.Length), fixedTestCode, CancellationToken.None).ConfigureAwait(false);
298321
}
299322
}
300323
}

0 commit comments

Comments
 (0)