Skip to content

Commit a8920aa

Browse files
committed
Update SA1205 for partial methods
1 parent ff14d11 commit a8920aa

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/OrderingRules/SA1205CSharp9UnitTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,80 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.OrderingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.OrderingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.OrderingRules.SA1205PartialElementsMustDeclareAccess,
13+
StyleCop.Analyzers.OrderingRules.SA1205CodeFixProvider>;
714

815
public partial class SA1205CSharp9UnitTests : SA1205CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3971, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3971")]
19+
public async Task TestPartialMethodWithPublicAccessModifierAsync()
20+
{
21+
var testCode = @"
22+
public partial class TestClass
23+
{
24+
public partial int TestMethod();
25+
}
26+
27+
public partial class TestClass
28+
{
29+
public partial int TestMethod()
30+
{
31+
return 0;
32+
}
33+
}
34+
";
35+
36+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
37+
}
38+
39+
[Fact]
40+
[WorkItem(3971, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3971")]
41+
public async Task TestPartialMethodWithoutAccessModifierAsync()
42+
{
43+
var testCode = @"
44+
public partial class TestClass
45+
{
46+
partial void {|#0:TestMethod|}();
47+
}
48+
49+
public partial class TestClass
50+
{
51+
public partial void {|CS8799:TestMethod|}()
52+
{
53+
}
54+
}
55+
";
56+
57+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
58+
}
59+
60+
[Fact]
61+
[WorkItem(3971, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3971")]
62+
public async Task TestPartialMethodWithoutAccessModifierNonVoidAsync()
63+
{
64+
var testCode = @"
65+
public partial class TestClass
66+
{
67+
partial int {|CS8796:TestMethod|}();
68+
}
69+
70+
public partial class TestClass
71+
{
72+
public partial int {|CS8799:TestMethod|}()
73+
{
74+
return 0;
75+
}
76+
}
77+
";
78+
79+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
80+
}
1081
}
1182
}

documentation/SA1205.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ The partial element does not have an access modifier defined.
2323

2424
A violation of this rule occurs when the partial elements does not have an access modifier defined.
2525

26+
### Partial methods in C# 9
27+
28+
C# 9 allows partial methods to have access modifiers and non-`void` return types. For compatibility across versions,
29+
SA1205 does not require these access modifiers to be included. In addition, the compiler enforces the presence of access
30+
modifiers when using the new features, so SA1205 will not apply duplicate diagnostics in cases where required modifiers
31+
are omitted.
32+
2633
## How to fix violations
2734

2835
To fix an instance of this violation, specify an access modifier for the partial element.

0 commit comments

Comments
 (0)