Skip to content

Commit

Permalink
clean up wrong nullability annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz committed Jan 8, 2025
1 parent a8b253b commit 9ef42f0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 73 deletions.
2 changes: 1 addition & 1 deletion FastCloner.Tests/ConcurrentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void CloneObject_WithConcurrentAccess_GeneratesOnlyOneCloner()
{
foreach (Task<TestClass> task in tasks)
{
TestClass? clone = task.Result;
TestClass clone = task.Result;
Assert.That(clone.Value, Is.EqualTo(42));
}
});
Expand Down
16 changes: 8 additions & 8 deletions FastCloner.Tests/CtorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public void GetOrAdd_ParallelAccess_ShouldBeThreadSafe()
{
// Arrange
int iterations = 1000;
List<Task>? parallelTasks = [];
List<Task> parallelTasks = [];
ConcurrentDictionary<Type, string> typeCache = new ConcurrentDictionary<Type, string>();

// Act
for (int i = 0; i < iterations; i++)
{
Task? task = Task.Run(() =>
Task task = Task.Run(() =>
{
string? value = typeCache.GetOrAdd(typeof(string), t =>
string value = typeCache.GetOrAdd(typeof(string), t =>
{
Thread.Sleep(10);
return "computed value";
Expand All @@ -71,21 +71,21 @@ public void GetOrAdd_ParallelAccess_ShouldBeThreadSafe()
[Test]
public void Object_With_Private_Constructor_Should_Be_Cloned()
{
T1? t1 = T1.Create();
T1 t1 = T1.Create();
t1.X = 42;
T1? cloned = t1.DeepClone();
T1 cloned = t1.DeepClone();
t1.X = 0;
Assert.That(cloned.X, Is.EqualTo(42));
}

[Test]
public void Object_With_Complex_Constructor_Should_Be_Cloned()
{
T2? t2 = new T2(1, 2)
T2 t2 = new T2(1, 2)
{
X = 42
};
T2? cloned = t2.DeepClone();
T2 cloned = t2.DeepClone();
t2.X = 0;
Assert.That(cloned.X, Is.EqualTo(42));
}
Expand All @@ -103,7 +103,7 @@ public void Anonymous_Object_Should_Be_Cloned()
public void Cloner_Should_Not_Call_Any_Method_Of_Class_Be_Cloned()
{
Assert.DoesNotThrow(() => new ExClass("x").DeepClone());
ExClass? exClass = new ExClass("x");
ExClass exClass = new ExClass("x");
Assert.DoesNotThrow(() => new[] { exClass, exClass }.DeepClone());
}
}
44 changes: 22 additions & 22 deletions FastCloner.Tests/InheritanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public class C3
[Test]
public void Descendant_Should_Be_Cloned()
{
C2? c2 = new C2();
C2 c2 = new C2();
c2.X = 1;
c2.Y = 2;
c2.Z = 3;
C1 c1 = c2;
c1.X = 4;
C1? cloned = c1.DeepClone();
C1 cloned = c1.DeepClone();
Assert.That(cloned, Is.TypeOf<C2>());
Assert.That(cloned.X, Is.EqualTo(4));
Assert.That(cloned.Y, Is.EqualTo(2));
Expand All @@ -99,13 +99,13 @@ public void Descendant_Should_Be_Cloned()
[Test]
public void Class_Should_Be_Cloned_With_Parents()
{
C2P? c2 = new C2P();
C2P c2 = new C2P();
c2.X = 1;
c2.Y = 2;
c2.Z = 3;
C1P c1 = c2;
c1.X = 4;
C2P? cloned = c2.DeepClone();
C2P cloned = c2.DeepClone();
c2.X = 100;
c2.Y = 100;
c2.Z = 100;
Expand Down Expand Up @@ -158,11 +158,11 @@ public void Struct_Should_Be_Cloned_With_Class_With_Parents()
[Test]
public void Descendant_In_Array_Should_Be_Cloned()
{
C1? c1 = new C1();
C2? c2 = new C2();
C1[]? arr = [c1, c2];
C1 c1 = new C1();
C2 c2 = new C2();
C1[] arr = [c1, c2];

C1[]? cloned = arr.DeepClone();
C1[] cloned = arr.DeepClone();
Assert.That(cloned[0], Is.TypeOf<C1>());
Assert.That(cloned[1], Is.TypeOf<C2>());
}
Expand All @@ -188,12 +188,12 @@ public IDisposable CCC(IDisposable xx)
[Test]
public void Class_Casted_To_Object_Should_Be_Cloned()
{
C3? c3 = new C3
C3 c3 = new C3
{
X = new C1()
};
object obj = c3;
object? cloned = obj.DeepClone();
object cloned = obj.DeepClone();
Assert.That(cloned, Is.TypeOf<C3>());
Assert.That(c3, Is.Not.EqualTo(cloned));
Assert.That(((C3)cloned).X, Is.Not.Null);
Expand All @@ -203,9 +203,9 @@ public void Class_Casted_To_Object_Should_Be_Cloned()
[Test]
public void Class_Casted_To_Interface_Should_Be_Cloned()
{
C1? c1 = new C1();
C1 c1 = new C1();
IDisposable disp = c1;
IDisposable? cloned = disp.DeepClone();
IDisposable cloned = disp.DeepClone();
Assert.That(c1, Is.Not.EqualTo(cloned));
Assert.That(cloned, Is.TypeOf<C1>());
}
Expand All @@ -226,8 +226,8 @@ public void Struct_Casted_To_Interface_With_Class_As_Interface_Should_Be_Cloned(
public void Array_Of_Struct_Casted_To_Interface_Should_Be_Cloned()
{
S1 s1 = new S1();
IDisposable[]? arr = [s1, s1];
IDisposable[]? clonedArr = arr.DeepClone();
IDisposable[] arr = [s1, s1];
IDisposable[] clonedArr = arr.DeepClone();
Assert.That(clonedArr[0], Is.EqualTo(clonedArr[1]));
}

Expand Down Expand Up @@ -265,45 +265,45 @@ public V2(string x)
[Test]
public void Class_With_Safe_Class_Should_Be_Cloned()
{
V1? v = new V1
V1 v = new V1
{
Safe = new Safe1()
};
V1? v2 = v.DeepClone();
V1 v2 = v.DeepClone();
Assert.That(v.Safe == v2.Safe, Is.False);
}

[Test]
public void Class_With_Safe_Class_Should_Be_Cloned_No_Default_Constructor()
{
V2? v = new V2("X")
V2 v = new V2("X")
{
Safe = new Safe1()
};
V2? v2 = v.DeepClone();
V2 v2 = v.DeepClone();
Assert.That(v.Safe == v2.Safe, Is.False);
}

[Test]
public void Class_With_UnSafe_Class_Should_Be_Cloned()
{
V1? v = new V1
V1 v = new V1
{
Safe = new Unsafe1()
};
V1? v2 = v.DeepClone();
V1 v2 = v.DeepClone();
Assert.That(v.Safe == v2.Safe, Is.False);
Assert.That(v2.Safe.GetType(), Is.EqualTo(typeof(Unsafe1)));
}

[Test]
public void Class_With_UnSafe_Class_Should_Be_Cloned_No_Default_Constructor()
{
V2? v = new V2("X")
V2 v = new V2("X")
{
Safe = new Unsafe1()
};
V2? v2 = v.DeepClone();
V2 v2 = v.DeepClone();
Assert.That(v.Safe == v2.Safe, Is.False);
Assert.That(v2.Safe.GetType(), Is.EqualTo(typeof(Unsafe1)));
}
Expand Down
6 changes: 3 additions & 3 deletions FastCloner/Code/AhoCorasick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void BuildTrie()

private void BuildFailureLinks()
{
Queue<Node>? queue = new Queue<Node>();
Queue<Node> queue = new Queue<Node>();
foreach (Node? node in root.Children.Values)
{
node.Failure = root;
Expand All @@ -49,10 +49,10 @@ private void BuildFailureLinks()

while (queue.Count > 0)
{
Node? current = queue.Dequeue();
Node current = queue.Dequeue();
foreach (KeyValuePair<char, Node> kvp in current.Children)
{
Node? child = kvp.Value;
Node child = kvp.Value;
char character = kvp.Key;
queue.Enqueue(child);

Expand Down
52 changes: 26 additions & 26 deletions FastCloner/Code/ClonerToExprGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
return GenerateProcessArrayMethod(type, isDeepClone);
}

Type? methodType = typeof(object);
Type methodType = typeof(object);

List<Expression>? expressionList = [];
List<Expression> expressionList = [];

ParameterExpression from = Expression.Parameter(methodType);
ParameterExpression? fromLocal = from;
ParameterExpression? to = Expression.Parameter(methodType);
ParameterExpression? toLocal = to;
ParameterExpression? state = Expression.Parameter(typeof(FastCloneState));
ParameterExpression fromLocal = from;
ParameterExpression to = Expression.Parameter(methodType);
ParameterExpression toLocal = to;
ParameterExpression state = Expression.Parameter(typeof(FastCloneState));

// if (!type.IsValueType())
{
Expand Down Expand Up @@ -61,11 +61,11 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
{
if (isDeepClone && !FastClonerSafeTypes.CanReturnSameObject(fieldInfo.FieldType))
{
MethodInfo? methodInfo = fieldInfo.FieldType.IsValueType()
MethodInfo methodInfo = fieldInfo.FieldType.IsValueType()
? StaticMethodInfos.DeepClonerGeneratorMethods.CloneStructInternal.MakeGenericMethod(fieldInfo.FieldType)
: StaticMethodInfos.DeepClonerGeneratorMethods.CloneClassInternal;

MemberExpression? get = Expression.Field(fromLocal, fieldInfo);
MemberExpression get = Expression.Field(fromLocal, fieldInfo);

// toLocal.Field = Clone...Internal(fromLocal.Field)
Expression call = Expression.Call(methodInfo, get, state);
Expand All @@ -78,7 +78,7 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
{
// var setMethod = fieldInfo.GetType().GetMethod("SetValue", new[] { typeof(object), typeof(object) });
// expressionList.Add(Expression.Call(Expression.Constant(fieldInfo), setMethod, toLocal, call));
MethodInfo? setMethod = typeof(FastClonerExprGenerator).GetPrivateStaticMethod(nameof(FastClonerExprGenerator.ForceSetField))!;
MethodInfo setMethod = typeof(FastClonerExprGenerator).GetPrivateStaticMethod(nameof(FastClonerExprGenerator.ForceSetField))!;
expressionList.Add(Expression.Call(setMethod, Expression.Constant(fieldInfo),
Expression.Convert(toLocal, typeof(object)), Expression.Convert(call, typeof(object))));
}
Expand All @@ -95,9 +95,9 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)

expressionList.Add(Expression.Convert(toLocal, methodType));

Type? funcType = typeof(Func<,,,>).MakeGenericType(methodType, methodType, typeof(FastCloneState), methodType);
Type funcType = typeof(Func<,,,>).MakeGenericType(methodType, methodType, typeof(FastCloneState), methodType);

List<ParameterExpression>? blockParams = [];
List<ParameterExpression> blockParams = [];
if (from != fromLocal) blockParams.Add(fromLocal);
if (to != toLocal) blockParams.Add(toLocal);

Expand All @@ -106,31 +106,31 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)

private static object GenerateProcessArrayMethod(Type type, bool isDeep)
{
Type? elementType = type.GetElementType()!;
Type elementType = type.GetElementType()!;
int rank = type.GetArrayRank();

ParameterExpression from = Expression.Parameter(typeof(object));
ParameterExpression to = Expression.Parameter(typeof(object));
ParameterExpression? state = Expression.Parameter(typeof(FastCloneState));
ParameterExpression state = Expression.Parameter(typeof(FastCloneState));

Type? funcType = typeof(Func<,,,>).MakeGenericType(typeof(object), typeof(object), typeof(FastCloneState), typeof(object));
Type funcType = typeof(Func<,,,>).MakeGenericType(typeof(object), typeof(object), typeof(FastCloneState), typeof(object));

if (rank == 1 && type == elementType.MakeArrayType())
{
if (!isDeep)
{
MethodCallExpression? callS = Expression.Call(
MethodCallExpression callS = Expression.Call(
typeof(ClonerToExprGenerator).GetPrivateStaticMethod(nameof(ShallowClone1DimArraySafeInternal))!
.MakeGenericMethod(elementType), Expression.Convert(from, type), Expression.Convert(to, type));
return Expression.Lambda(funcType, callS, from, to, state).Compile();
}
else
{
string? methodName = nameof(Clone1DimArrayClassInternal);
string methodName = nameof(Clone1DimArrayClassInternal);
if (FastClonerSafeTypes.CanReturnSameObject(elementType)) methodName = nameof(Clone1DimArraySafeInternal);
else if (elementType.IsValueType()) methodName = nameof(Clone1DimArrayStructInternal);
MethodInfo? methodInfo = typeof(ClonerToExprGenerator).GetPrivateStaticMethod(methodName)!.MakeGenericMethod(elementType);
MethodCallExpression? callS = Expression.Call(methodInfo, Expression.Convert(from, type), Expression.Convert(to, type), state);
MethodInfo methodInfo = typeof(ClonerToExprGenerator).GetPrivateStaticMethod(methodName)!.MakeGenericMethod(elementType);
MethodCallExpression callS = Expression.Call(methodInfo, Expression.Convert(from, type), Expression.Convert(to, type), state);
return Expression.Lambda(funcType, callS, from, to, state).Compile();
}
}
Expand All @@ -143,7 +143,7 @@ private static object GenerateProcessArrayMethod(Type type, bool isDeep)
else
methodInfo = typeof(ClonerToExprGenerator).GetPrivateStaticMethod(nameof(CloneAbstractArrayInternal))!;

MethodCallExpression? callS = Expression.Call(methodInfo, Expression.Convert(from, type), Expression.Convert(to, type), state, Expression.Constant(isDeep));
MethodCallExpression callS = Expression.Call(methodInfo, Expression.Convert(from, type), Expression.Convert(to, type), state, Expression.Constant(isDeep));
return Expression.Lambda(funcType, callS, from, to, state).Compile();
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, FastCl
if (objFrom == null || objTo == null) return null;
int l = Math.Min(objFrom.Length, objTo.Length);
state.AddKnownRef(objFrom, objTo);
Func<T, FastCloneState, T>? cloner = FastClonerGenerator.GetClonerForValueType<T>();
Func<T, FastCloneState, T> cloner = FastClonerGenerator.GetClonerForValueType<T>();
for (int i = 0; i < l; i++)
objTo[i] = cloner(objTo[i], state);

Expand Down Expand Up @@ -219,7 +219,7 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, FastCl

if (typeof(T).IsValueType())
{
Func<T, FastCloneState, T>? cloner = FastClonerGenerator.GetClonerForValueType<T>();
Func<T, FastCloneState, T> cloner = FastClonerGenerator.GetClonerForValueType<T>();
for (int i = 0; i < l1; i++)
for (int k = 0; k < l2; k++)
objTo[i, k] = cloner(objFrom[i, k], state);
Expand All @@ -243,11 +243,11 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, FastCl

if (objTo.Rank != rank)
throw new InvalidOperationException("Invalid rank of target array");
int[]? lowerBoundsFrom = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
int[]? lowerBoundsTo = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();
int[]? lengths = Enumerable.Range(0, rank).Select(x => Math.Min(objFrom.GetLength(x), objTo.GetLength(x))).ToArray();
int[]? idxesFrom = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
int[]? idxesTo = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();
int[] lowerBoundsFrom = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
int[] lowerBoundsTo = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();
int[] lengths = Enumerable.Range(0, rank).Select(x => Math.Min(objFrom.GetLength(x), objTo.GetLength(x))).ToArray();
int[] idxesFrom = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
int[] idxesTo = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();

state.AddKnownRef(objFrom, objTo);

Expand Down
Loading

0 comments on commit 9ef42f0

Please sign in to comment.