Skip to content

Commit 2ba3c91

Browse files
committed
Update SA1214 for readonly members
1 parent 4811f54 commit 2ba3c91

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,55 @@
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.SA1214ReadonlyElementsMustAppearBeforeNonReadonlyElements,
13+
StyleCop.Analyzers.OrderingRules.ElementOrderCodeFixProvider>;
714

815
public partial class SA1214CSharp8UnitTests : SA1214CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
19+
public async Task VerifyReadonlyStructMethodsAreIgnoredAsync()
20+
{
21+
var testCode = @"
22+
public struct S
23+
{
24+
public void M1() { }
25+
public readonly void M2() { }
26+
public void M3() { }
27+
}";
28+
29+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
30+
}
31+
32+
[Fact]
33+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
34+
public async Task VerifyReadonlyAccessorsAreIgnoredAsync()
35+
{
36+
var testCode = @"
37+
public struct S
38+
{
39+
private int backingField;
40+
41+
public int Value
42+
{
43+
readonly get => this.backingField;
44+
set => this.backingField = value;
45+
}
46+
47+
public int Other
48+
{
49+
get => this.backingField;
50+
set => this.backingField = value;
51+
}
52+
}";
53+
54+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
55+
}
1056
}
1157
}

documentation/SA1214.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ A readonly field is positioned beneath a non-readonly field.
2323

2424
A violation of this rule occurs when a readonly field is positioned beneath a non-readonly field.
2525

26+
This rule applies only to field declarations. The C# 8 readonly members feature (e.g. readonly struct methods, properties, indexers, or accessors) is not covered by SA1214; those members follow the normal ordering rules for their declaration kind.
27+
2628
## How to fix violations
2729

2830
To fix an instance of this violation, place all readonly fields above all non-readonly fields.

0 commit comments

Comments
 (0)