Skip to content

Commit 414e3fd

Browse files
committed
Update SA1204 for default interface members
1 parent b72b3f7 commit 414e3fd

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/OrderingRules/SA1204CSharp8UnitTests.cs

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

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

815
public partial class SA1204CSharp8UnitTests : SA1204CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3002, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3002")]
19+
public async Task TestStaticMemberAfterDefaultInterfaceMethodAsync()
20+
{
21+
var testCode = @"public interface ITest
22+
{
23+
void InstanceMethod()
24+
{
25+
}
26+
27+
static void [|StaticMethod|]()
28+
{
29+
}
30+
}
31+
";
32+
33+
var fixedCode = @"public interface ITest
34+
{
35+
static void StaticMethod()
36+
{
37+
}
38+
39+
void InstanceMethod()
40+
{
41+
}
42+
}
43+
";
44+
45+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
46+
}
1047
}
1148
}

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1204StaticElementsMustAppearBeforeInstanceElements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal class SA1204StaticElementsMustAppearBeforeInstanceElements : Diagnostic
4141
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.OrderingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
4242

4343
private static readonly ImmutableArray<SyntaxKind> TypeDeclarationKinds =
44-
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration);
44+
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration);
4545

4646
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> CompilationUnitAction = HandleCompilationUnit;
4747
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> BaseNamespaceDeclarationAction = HandleBaseNamespaceDeclaration;

documentation/SA1204.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ A static element is positioned beneath an instance element of the same type.
2323

2424
A violation of this rule occurs when a static element is positioned beneath an instance element of the same type. All static elements should be placed above all instance elements of the same type to make it easier to see the interface exposed from the instance and static version of the class.
2525

26+
This rule applies to classes, structs, and interfaces. Static interface members (introduced with C# 8 default interface members) must appear before instance members, including members that provide default implementations.
27+
28+
For example, the following interface declaration violates this rule because the static member appears after an instance member with a default implementation:
29+
30+
```csharp
31+
public interface IExample
32+
{
33+
void Instance() { }
34+
static void Helper() { } // SA1204
35+
}
36+
```
37+
38+
A compliant ordering places static interface members before instance members:
39+
40+
```csharp
41+
public interface IExample
42+
{
43+
static void Helper() { }
44+
void Instance() { }
45+
}
46+
```
47+
2648
## How to fix violations
2749

2850
To fix an instance of this violation, place all static elements above all instance elements of the same type.

0 commit comments

Comments
 (0)