Skip to content

Commit bd74247

Browse files
committed
Analyzer is using IDisallowDefaultValue for implementation of TTRE47 (VariableMustBeInitializedWithNonDefaultValue)
1 parent 2aeb10d commit bd74247

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Diagnostics/ThinktectureRuntimeExtensionsAnalyzer.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static void MemberDisallowingDefaultValuesMustBeRequired(
121121
if (memberType.SpecialType != SpecialType.None || !memberType.IsValueType)
122122
return;
123123

124-
if (!memberType.Interfaces.Any(i => i.IsIDisallowDefaultValue()))
124+
if (!memberType.ImplementsIDisallowDefaultValue())
125125
return;
126126

127127
context.ReportDiagnostic(Diagnostic.Create(
@@ -177,7 +177,7 @@ private static void AnalyzeMethodWithUseDelegateFromConstructor(OperationAnalysi
177177
}
178178
}
179179

180-
private void AnalyzeObjectCreation(OperationAnalysisContext context)
180+
private static void AnalyzeObjectCreation(OperationAnalysisContext context)
181181
{
182182
var operation = (IObjectCreationOperation)context.Operation;
183183

@@ -186,14 +186,11 @@ private void AnalyzeObjectCreation(OperationAnalysisContext context)
186186
|| operation.Arguments.Length > 0)
187187
return;
188188

189-
if (operation.Type.IsAdHocUnionType(out _)
190-
|| (operation.Type.IsValueObjectType(out var valueObjectAttributeBase) && !valueObjectAttributeBase.FindAllowDefaultStructs()))
191-
{
189+
if (operation.Type.ImplementsIDisallowDefaultValue())
192190
ReportDiagnostic(context, DiagnosticsDescriptors.VariableMustBeInitializedWithNonDefaultValue, operation.Syntax.GetLocation(), operation.Type);
193-
}
194191
}
195192

196-
private void AnalyzeDefaultValueAssignment(OperationAnalysisContext context)
193+
private static void AnalyzeDefaultValueAssignment(OperationAnalysisContext context)
197194
{
198195
var operation = (IDefaultValueOperation)context.Operation;
199196

@@ -204,11 +201,8 @@ private void AnalyzeDefaultValueAssignment(OperationAnalysisContext context)
204201
return;
205202
}
206203

207-
if (operation.Type.IsAdHocUnionType(out _)
208-
|| (operation.Type.IsValueObjectType(out var valueObjectAttributeBase) && !valueObjectAttributeBase.FindAllowDefaultStructs()))
209-
{
204+
if (operation.Type.ImplementsIDisallowDefaultValue())
210205
ReportDiagnostic(context, DiagnosticsDescriptors.VariableMustBeInitializedWithNonDefaultValue, operation.Syntax.GetLocation(), operation.Type);
211-
}
212206
}
213207

214208
private static bool IsAssignmentOrInitialization(IOperation? operation)
@@ -527,7 +521,7 @@ private static void ValidateKeyedValueObject(
527521
{
528522
ReportDiagnostic(context, DiagnosticsDescriptors.AllowDefaultStructsCannotBeTrueIfValueObjectIsStructButKeyTypeIsClass, attribute.Syntax.GetLocation(), type, keyType);
529523
}
530-
else if (keyType.AllInterfaces.Any(i => i.IsIDisallowDefaultValue()))
524+
else if (keyType.ImplementsIDisallowDefaultValue())
531525
{
532526
ReportDiagnostic(context,
533527
DiagnosticsDescriptors.AllowDefaultStructsCannotBeTrueIfSomeMembersDisallowDefaultValues,

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/InstanceMemberInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private InstanceMemberInfo(
127127

128128
private static bool IsDisallowingDefaultValue(ITypeSymbol type)
129129
{
130-
if (type.AllInterfaces.Any(i => i.IsIDisallowDefaultValue()))
130+
if (type.ImplementsIDisallowDefaultValue())
131131
return true;
132132

133133
if (type.IsReferenceType

src/Thinktecture.Runtime.Extensions.SourceGenerator/Extensions/TypeSymbolExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,4 +844,17 @@ public static bool IsIDisallowDefaultValue(this ITypeSymbol? type)
844844
{
845845
return type is { Name: Constants.Interfaces.DisallowDefaultStructs.NAME, ContainingNamespace: { Name: Constants.Interfaces.DisallowDefaultStructs.NAMESPACE, ContainingNamespace.IsGlobalNamespace: true } };
846846
}
847+
848+
public static bool ImplementsIDisallowDefaultValue(this ITypeSymbol type)
849+
{
850+
for (var i = 0; i < type.AllInterfaces.Length; i++)
851+
{
852+
var @interface = type.AllInterfaces[i];
853+
854+
if (@interface.IsIDisallowDefaultValue())
855+
return true;
856+
}
857+
858+
return false;
859+
}
847860
}

test/Thinktecture.Runtime.Extensions.Json.Tests/Text/Json/Serialization/ValueObjectJsonConverterFactoryTests/WriteJson.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public void Should_deserialize_enum_if_null_and_default()
3535
SerializeWithConverter<TestSmartEnum_Struct_IntBased_Validatable?, ValueObjectJsonConverterFactory<TestSmartEnum_Struct_IntBased_Validatable, int, ValidationError>>(null).Should().Be("null");
3636
SerializeWithConverter<TestSmartEnum_Struct_StringBased_Validatable?, ValueObjectJsonConverterFactory<TestSmartEnum_Struct_StringBased_Validatable, string, ValidationError>>(null).Should().Be("null");
3737
SerializeWithConverter<TestSmartEnum_Struct_IntBased_Validatable, ValueObjectJsonConverterFactory<TestSmartEnum_Struct_IntBased_Validatable, int, ValidationError>>(default).Should().Be("0");
38-
SerializeWithConverter<TestSmartEnum_Struct_StringBased_Validatable, ValueObjectJsonConverterFactory<TestSmartEnum_Struct_StringBased_Validatable, string, ValidationError>>(default).Should().Be("null");
3938
}
4039

4140
[Fact]

test/Thinktecture.Runtime.Extensions.Newtonsoft.Json.Tests/Json/ValueObjectNewtonsoftJsonConverterTests/WriteJson.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public void Should_deserialize_enum_if_null_and_default()
3131
SerializeNullableStruct<TestSmartEnum_Struct_IntBased_Validatable, int>(null).Should().Be("null");
3232
SerializeNullableStruct<TestSmartEnum_Struct_StringBased_Validatable, string>(null).Should().Be("null");
3333
SerializeStruct<TestSmartEnum_Struct_IntBased_Validatable, int>(default).Should().Be("0");
34-
SerializeStruct<TestSmartEnum_Struct_StringBased_Validatable, string>(default).Should().Be("null");
3534
}
3635

3736
[Fact]

test/Thinktecture.Runtime.Extensions.Tests/EnumTests/EnsureValid.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ public void Should_throw_if_default_struct_is_invalid()
3434
{
3535
new StructIntegerEnum().Invoking(e => e.EnsureValid())
3636
.Should().Throw<InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(StructIntegerEnum)}\" with identifier \"0\" is not valid.");
37-
38-
new StructStringEnum().Invoking(e => e.EnsureValid())
39-
.Should().Throw<InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(StructStringEnum)}\" with identifier \"\" is not valid.");
4037
}
4138

4239
[Fact]

0 commit comments

Comments
 (0)