Skip to content

Commit 38c9d10

Browse files
committed
Update SA1401 for readonly members
1 parent 962472b commit 38c9d10

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1401CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.MaintainabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<
12+
StyleCop.Analyzers.MaintainabilityRules.SA1401FieldsMustBePrivate>;
713

814
public partial class SA1401CSharp8UnitTests : SA1401CSharp7UnitTests
915
{
16+
[Theory]
17+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
18+
[InlineData("public")]
19+
[InlineData("internal")]
20+
[InlineData("protected")]
21+
[InlineData("protected internal")]
22+
public async Task TestClassWithReadonlyFieldAsync(string accessModifier)
23+
{
24+
var testCode = $@"public class Foo
25+
{{
26+
{accessModifier} readonly string [|bar|];
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 VerifyReadonlyStructMembersAreIgnoredAsync()
35+
{
36+
var testCode = @"
37+
public struct S
38+
{
39+
public readonly int Property { get; }
40+
public readonly void Method() { }
41+
}";
42+
43+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
}
1045
}
1146
}

documentation/SA1401.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ A violation of this rule occurs whenever a field in a class is given non-private
2525

2626
Fields located within C# structs are allowed to have any access level.
2727

28-
Fields that are static and readonly will not raise a violation. These kinds of fields are commonly used to represent a constant value when the `const` keyword cannot be used, and therefore they are exempt from this rule.
28+
Fields that are static and readonly will not raise a violation. These kinds of fields are commonly used to represent a constant value when the `const` keyword cannot be used, and therefore they are exempt from this rule. Instance fields are not exempt just because they are readonly; they still need to be private to satisfy this rule.
29+
30+
The C# 8 `readonly` modifier for struct members (methods, properties, indexers, and events) does not affect this rule because it only considers fields.
2931

3032
## How to fix violations
3133

0 commit comments

Comments
 (0)