Skip to content

Commit 83f51e0

Browse files
committed
Update SA1649 for readonly members
1 parent d1df28b commit 83f51e0

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/DocumentationRules/SA1649CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.DocumentationRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.DocumentationRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName>;
712

813
public partial class SA1649CSharp8UnitTests : SA1649CSharp7UnitTests
914
{
15+
[Fact]
16+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
17+
public async Task VerifyReadonlyMembersDoNotAffectCorrectFileNameAsync()
18+
{
19+
var testCode = @"
20+
namespace TestNamespace
21+
{
22+
public readonly struct TestType
23+
{
24+
private readonly int value;
25+
26+
public TestType(int value)
27+
{
28+
this.value = value;
29+
}
30+
31+
public readonly int GetValue() => this.value;
32+
33+
public int Property
34+
{
35+
readonly get => this.value;
36+
set => _ = value;
37+
}
38+
39+
public int this[int index]
40+
{
41+
readonly get => this.value + index;
42+
set => _ = value - index;
43+
}
44+
}
45+
}
46+
";
47+
48+
await VerifyCSharpDiagnosticAsync(fileName: "TestType.cs", testCode, testSettings: null, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
49+
}
50+
51+
[Fact]
52+
[WorkItem(3001, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3001")]
53+
public async Task VerifyWrongFileNameWithReadonlyMembersAsync()
54+
{
55+
var testCode = @"
56+
namespace TestNamespace
57+
{
58+
public readonly struct {|#0:TestType|}
59+
{
60+
private readonly int value;
61+
62+
public TestType(int value)
63+
{
64+
this.value = value;
65+
}
66+
67+
public readonly int GetValue() => this.value;
68+
69+
public int Property
70+
{
71+
readonly get => this.value;
72+
set => _ = value;
73+
}
74+
75+
public int this[int index]
76+
{
77+
readonly get => this.value + index;
78+
set => _ = value - index;
79+
}
80+
}
81+
}
82+
";
83+
var fixedCode = @"
84+
namespace TestNamespace
85+
{
86+
public readonly struct TestType
87+
{
88+
private readonly int value;
89+
90+
public TestType(int value)
91+
{
92+
this.value = value;
93+
}
94+
95+
public readonly int GetValue() => this.value;
96+
97+
public int Property
98+
{
99+
readonly get => this.value;
100+
set => _ = value;
101+
}
102+
103+
public int this[int index]
104+
{
105+
readonly get => this.value + index;
106+
set => _ = value - index;
107+
}
108+
}
109+
}
110+
";
111+
112+
var expectedDiagnostic = Diagnostic().WithLocation(0);
113+
await VerifyCSharpFixAsync(oldFileName: "WrongFileName.cs", testCode, StyleCopSettings, expectedDiagnostic, newFileName: "TestType.cs", fixedCode, CancellationToken.None).ConfigureAwait(false);
114+
}
10115
}
11116
}

documentation/SA1649.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class Class1
4343
}
4444
```
4545

46+
A `readonly` modifier on the type (for example a `readonly struct`) or on members inside the first type (such as C# 8
47+
readonly struct members) does not change the expected file name. The analyzer continues to use the identifier of the
48+
first non-partial type, so a type declared as `public readonly struct Point` still belongs in `Point.cs`.
49+
4650
## How to fix violations
4751

4852
To fix a violation of this rule, rename the file to match the name of the first type from the file.

0 commit comments

Comments
 (0)