Skip to content

Commit

Permalink
Enable RCS1169 and RCS1170 if the type is read-only struct
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Feb 10, 2020
1 parent 988d53f commit f34e105
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private static bool ValidateType(ITypeSymbol type)

return type.IsReferenceType
|| type.TypeKind == TypeKind.Enum
|| CSharpFacts.IsSimpleType(type.SpecialType);
|| CSharpFacts.IsSimpleType(type.SpecialType)
|| type.IsReadOnlyStruct();
}
}
}
42 changes: 39 additions & 3 deletions src/Tests/Analyzers.Tests/RCS1169MakeFieldReadOnlyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@ public C()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MakeFieldReadOnly)]
public async Task Test_InstanceField_ReadOnlyStruct()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
[|private B _f;|]
public C()
{
_f = default;
}
}
readonly struct B
{
}
", @"
class C
{
private readonly B _f;
public C()
{
_f = default;
}
}
readonly struct B
{
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.MakeFieldReadOnly)]
public async Task Test_StaticField()
{
Expand Down Expand Up @@ -148,11 +182,13 @@ void M()
public async Task TestNoDiagnostic_Struct()
{
await VerifyNoDiagnosticAsync(@"
using System;
class C
{
DateTime _f;
B _f;
}
struct B
{
}
");
}
Expand Down
46 changes: 41 additions & 5 deletions src/Tests/Analyzers.Tests/RCS1170UseReadOnlyAutoPropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class C
{
public int P { get; [|private set;|] }
public C()
public C()
{
P = 0;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ class C
{
public StringSplitOptions P { get; [|private set;|] }
public C()
public C()
{
P = 0;
}
Expand All @@ -103,6 +103,40 @@ public C()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseReadOnlyAutoProperty)]
public async Task Test_InstanceProperty_ReadOnlyStruct()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
public B P { get; [|private set;|] }
public C()
{
P = default;
}
}
readonly struct B
{
}
", @"
class C
{
public B P { get; }
public C()
{
P = default;
}
}
readonly struct B
{
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseReadOnlyAutoProperty)]
public async Task Test_StaticProperty()
{
Expand Down Expand Up @@ -181,11 +215,13 @@ void M()
public async Task TestNoDiagnostic_Struct()
{
await VerifyNoDiagnosticAsync(@"
using System;
class C
{
public DateTime P { get; private set; }
public B P { get; private set; }
}
struct B
{
}
");
}
Expand Down

0 comments on commit f34e105

Please sign in to comment.