Skip to content

Commit 975c0a6

Browse files
NikolayPianikovNikolayPianikov
authored andcommitted
Uses the keyword 'in' for read-only types only
1 parent f95de94 commit 975c0a6

File tree

9 files changed

+14
-7
lines changed

9 files changed

+14
-7
lines changed

Immutype.Benchmark/RecordWithFourProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Immutype.Benchmark
77
public record PersonRecordWithFourProperties(string? Name, int Age, string? LastName, bool HasPassport);
88

99
[Immutype.Target]
10-
public record struct PersonRecordStructWithFourProperties(string? Name, int Age, string? LastName, bool HasPassport);
10+
public readonly record struct PersonRecordStructWithFourProperties(string? Name, int Age, string? LastName, bool HasPassport);
1111

1212
[MemoryDiagnoser]
1313
[Orderer(SummaryOrderPolicy.FastestToSlowest)]

Immutype.Benchmark/RecordWithOneProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Immutype.Benchmark
77
public record PersonRecordWithOneProperty(string? Name);
88

99
[Immutype.Target]
10-
public record struct PersonRecordStructWithOneProperty(string? Name);
10+
public readonly record struct PersonRecordStructWithOneProperty(string? Name);
1111

1212
[MemoryDiagnoser]
1313
[Orderer(SummaryOrderPolicy.FastestToSlowest)]

Immutype.Benchmark/RecordWithTwoProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Immutype.Benchmark
77
public record PersonRecordWithTwoProperties(string? Name, int Age);
88

99
[Immutype.Target]
10-
public record struct PersonRecordStructWithTwoProperties(string? Name, int Age);
10+
public readonly record struct PersonRecordStructWithTwoProperties(string? Name, int Age);
1111

1212
[MemoryDiagnoser]
1313
[Orderer(SummaryOrderPolicy.FastestToSlowest)]

Immutype.UsageScenarios.Tests/ApplyingDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Immutype.UsageScenarios.Tests.ApplyingDefaults
1414
// $description=Applying defaults
1515
// {
1616
[Immutype.Target]
17-
internal record Person(string Name = "John", int Age = 17);
17+
internal readonly record struct Person(string Name = "John", int Age = 17);
1818

1919
public class ApplyingDefaults
2020
{

Immutype.UsageScenarios.Tests/Array.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Immutype.UsageScenarios.Tests.Array
1414
// $description=Array
1515
// {
1616
[Immutype.Target]
17-
internal record Person(string Name, int Age = 0, params Person[] Friends);
17+
internal readonly record struct Person(string Name, int Age = 0, params Person[] Friends);
1818

1919
public class Array
2020
{

Immutype.UsageScenarios.Tests/Removing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Immutype.UsageScenarios.Tests.Removing
1414
// $description=Removing
1515
// {
1616
[Immutype.Target]
17-
internal record Person(
17+
internal readonly record struct Person(
1818
string Name,
1919
int Age = 0,
2020
params Person[] Friends);

Immutype/Core/ISyntaxNodeFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Immutype.Core
77
internal interface ISyntaxNodeFactory
88
{
99
bool IsValueType(TypeDeclarationSyntax typeDeclarationSyntax);
10+
11+
bool IsReadonlyType(TypeDeclarationSyntax typeDeclarationSyntax);
1012

1113
bool HasTargetAttribute(MemberDeclarationSyntax memberDeclarationSyntax);
1214

Immutype/Core/MethodsFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public IEnumerable<MemberDeclarationSyntax> Create(GenerationContext<TypeDeclara
2525
.WithType(targetType)
2626
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ThisKeyword));
2727

28-
if (context.Options is CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp7_2 } && _syntaxNodeFactory.IsValueType(context.Syntax))
28+
if (
29+
context.Options is CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp7_2 }
30+
&& _syntaxNodeFactory.IsReadonlyType(context.Syntax))
2931
{
3032
thisParameter = thisParameter.AddModifiers(SyntaxFactory.Token(SyntaxKind.InKeyword));
3133
}

Immutype/Core/SyntaxNodeFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public bool IsValueType(TypeDeclarationSyntax typeDeclarationSyntax) =>
3636
_ => false
3737
};
3838

39+
public bool IsReadonlyType(TypeDeclarationSyntax typeDeclarationSyntax) =>
40+
typeDeclarationSyntax.Modifiers.Any(i => i.IsKind(SyntaxKind.ReadOnlyKeyword));
41+
3942
public bool HasTargetAttribute(MemberDeclarationSyntax memberDeclarationSyntax) =>
4043
memberDeclarationSyntax.AttributeLists
4144
.SelectMany(i => i.Attributes)

0 commit comments

Comments
 (0)