From 4b58bc267532acf5e7470522f46dc4f7346fc270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20St=C3=BChmer?= Date: Thu, 29 Oct 2026 03:08:12 +0100 Subject: [PATCH 1/2] refactor: remove unreachable default-value branches A boxed Nullable reports T from GetType(), so GetDefault never returns null for a value type and the '?? false' fallbacks in DefaultConstraint and NotDefaultConstraint cannot be reached. Activator.CreateInstance already returns null for Nullable, which makes the explicit Nullable block in TypeExtensions.GetDefault redundant. No test can kill mutants in this code, so remove it. Also rename the misnamed GetDefault test. --- .../Constraints/DefaultConstraint.cs | 2 +- .../Constraints/NotDefaultConstraint.cs | 2 +- src/NetEvolve.FluentValue/TypeExtensions.cs | 13 ++----------- .../TypeExtensionsTests.cs | 2 +- 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/NetEvolve.FluentValue/Constraints/DefaultConstraint.cs b/src/NetEvolve.FluentValue/Constraints/DefaultConstraint.cs index 24d0d01..c2d03b5 100644 --- a/src/NetEvolve.FluentValue/Constraints/DefaultConstraint.cs +++ b/src/NetEvolve.FluentValue/Constraints/DefaultConstraint.cs @@ -8,7 +8,7 @@ internal sealed class DefaultConstraint : ConstraintBase public override bool IsSatisfiedBy(object? value) => value?.GetType() switch { - { IsValueType: true } valueType => TypeExtensions.GetDefault(valueType)?.Equals(value) ?? false, + { IsValueType: true } valueType => Equals(TypeExtensions.GetDefault(valueType), value), _ => false, }; diff --git a/src/NetEvolve.FluentValue/Constraints/NotDefaultConstraint.cs b/src/NetEvolve.FluentValue/Constraints/NotDefaultConstraint.cs index 1ac1059..4fd65c9 100644 --- a/src/NetEvolve.FluentValue/Constraints/NotDefaultConstraint.cs +++ b/src/NetEvolve.FluentValue/Constraints/NotDefaultConstraint.cs @@ -8,7 +8,7 @@ internal sealed class NotDefaultConstraint : ConstraintBase public override bool IsSatisfiedBy(object? value) => value?.GetType() switch { - { IsValueType: true } valueType => !TypeExtensions.GetDefault(valueType)?.Equals(value) ?? false, + { IsValueType: true } valueType => !Equals(TypeExtensions.GetDefault(valueType), value), _ => true, }; diff --git a/src/NetEvolve.FluentValue/TypeExtensions.cs b/src/NetEvolve.FluentValue/TypeExtensions.cs index c8184e1..a88bb1a 100644 --- a/src/NetEvolve.FluentValue/TypeExtensions.cs +++ b/src/NetEvolve.FluentValue/TypeExtensions.cs @@ -6,16 +6,7 @@ internal static class TypeExtensions { internal static object? GetDefault(this Type value) { - if (!value.IsValueType) - { - return null; - } - - var underlying = Nullable.GetUnderlyingType(value); - if (underlying is not null) - { - return null; - } - return Activator.CreateInstance(value)!; + // Nullable yields null from Activator.CreateInstance, so no explicit handling is needed. + return value.IsValueType ? Activator.CreateInstance(value) : null; } } diff --git a/tests/NetEvolve.FluentValue.Tests.Unit/TypeExtensionsTests.cs b/tests/NetEvolve.FluentValue.Tests.Unit/TypeExtensionsTests.cs index 1bb5a55..390cf7e 100644 --- a/tests/NetEvolve.FluentValue.Tests.Unit/TypeExtensionsTests.cs +++ b/tests/NetEvolve.FluentValue.Tests.Unit/TypeExtensionsTests.cs @@ -7,7 +7,7 @@ public class TypeExtensionsTests { [Test] [MethodDataSource(nameof(GetDefaultData))] - public async Task IsNullableType_ShouldReturnTrue_WhenTypeIsNullable(object? expected, Type type) + public async Task GetDefault_ShouldReturnExpected(object? expected, Type type) { // Arrange & Act var result = TypeExtensions.GetDefault(type); From ed0d3e4988668a1ce4f02858d87a1a3d75ab83d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20St=C3=BChmer?= Date: Thu, 29 Oct 2026 17:11:33 +0100 Subject: [PATCH 2/2] style: use expression body for GetDefault --- src/NetEvolve.FluentValue/TypeExtensions.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/NetEvolve.FluentValue/TypeExtensions.cs b/src/NetEvolve.FluentValue/TypeExtensions.cs index a88bb1a..23353a3 100644 --- a/src/NetEvolve.FluentValue/TypeExtensions.cs +++ b/src/NetEvolve.FluentValue/TypeExtensions.cs @@ -4,9 +4,6 @@ internal static class TypeExtensions { - internal static object? GetDefault(this Type value) - { - // Nullable yields null from Activator.CreateInstance, so no explicit handling is needed. - return value.IsValueType ? Activator.CreateInstance(value) : null; - } + // Nullable yields null from Activator.CreateInstance, so no explicit handling is needed. + internal static object? GetDefault(this Type value) => value.IsValueType ? Activator.CreateInstance(value) : null; }