Skip to content

Commit d0ffa7c

Browse files
committed
Merge pull request #2051 from vweijsters/fix-2040
Fixed SA1205 issue with nested partial types
2 parents 020a41a + bf08014 commit d0ffa7c

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ public partial class Foo
162162
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
163163
}
164164

165+
/// <summary>
166+
/// Verifies that all 5 access modifiers are accepted for nested types.
167+
/// This is a regression test for issue #2040.
168+
/// </summary>
169+
/// <param name="accessModifier">The access modifier to use for the nested type.</param>
170+
/// <param name="typeKeyword">The type keyword to use.</param>
171+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
172+
[Theory]
173+
[InlineData("public", "class")]
174+
[InlineData("protected", "class")]
175+
[InlineData("internal", "class")]
176+
[InlineData("protected internal", "class")]
177+
[InlineData("private", "class")]
178+
[InlineData("public", "struct")]
179+
[InlineData("protected", "struct")]
180+
[InlineData("internal", "struct")]
181+
[InlineData("protected internal", "struct")]
182+
[InlineData("private", "struct")]
183+
public async Task TestNestedTypeAccessModifiersAsync(string accessModifier, string typeKeyword)
184+
{
185+
var testCode = $@"
186+
internal static partial class TestPartial
187+
{{
188+
{accessModifier} partial {typeKeyword} PartialInner
189+
{{
190+
public int Do()
191+
{{
192+
return 2;
193+
}}
194+
}}
195+
}}
196+
";
197+
198+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
199+
}
200+
165201
/// <inheritdoc/>
166202
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
167203
{

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1205PartialElementsMustDeclareAccess.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
5858
if (typeDeclarationNode.Modifiers.Any(SyntaxKind.PartialKeyword))
5959
{
6060
if (!typeDeclarationNode.Modifiers.Any(SyntaxKind.PublicKeyword)
61-
&& !typeDeclarationNode.Modifiers.Any(SyntaxKind.InternalKeyword))
61+
&& !typeDeclarationNode.Modifiers.Any(SyntaxKind.InternalKeyword)
62+
&& !typeDeclarationNode.Modifiers.Any(SyntaxKind.ProtectedKeyword)
63+
&& !typeDeclarationNode.Modifiers.Any(SyntaxKind.PrivateKeyword))
6264
{
6365
context.ReportDiagnostic(Diagnostic.Create(Descriptor, typeDeclarationNode.Identifier.GetLocation()));
6466
}

0 commit comments

Comments
 (0)