Skip to content

Commit

Permalink
Fix nullability warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslevesque committed Jun 23, 2021
1 parent a711054 commit 4a148b7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Linq.Extras/Flatten.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static IEnumerable<TResult> DepthFirstFlattenIterator<TNode, TResult>(th
var list = source.Select(n => new NodeWithLevel<TNode>(n, 0)).ToLinkedList();
while (list.Count > 0)
{
var current = list.First.Value;
var current = list.First!.Value;
list.RemoveFirst();
yield return resultSelector(current.Node, current.Level);
var llNode = list.First;
Expand Down
6 changes: 3 additions & 3 deletions src/Linq.Extras/Linq.Extras.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<AssemblyOriginatorKeyFile>..\..\Linq.Extras.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<MinVerVerbosity>quiet</MinVerVerbosity>
<LangVersion>8.0</LangVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand All @@ -29,11 +29,11 @@
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<DefineConstants>$(DefineConstants);FEATURE_NULLABILITY_ATTRIBUTES</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_NULLABILITY_ATTRIBUTES;FEATURE_COMPARER_NULLABILITY</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net5.0'">
<DefineConstants>$(DefineConstants);FEATURE_NULLABILITY_ATTRIBUTES</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_NULLABILITY_ATTRIBUTES;FEATURE_COMPARER_NULLABILITY</DefineConstants>
</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions src/Linq.Extras/OuterJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static IEnumerable<TResult> LeftOuterJoin<TLeft, TRight, TKey, TResult>(
[NotNull] Func<TLeft, TKey> leftKeySelector,
[NotNull] Func<TRight, TKey> rightKeySelector,
[NotNull] Func<TLeft, TRight, TResult> resultSelector,
TRight defaultRight = default,
TRight defaultRight = default!,
IEqualityComparer<TKey>? keyComparer = null)
{
left.CheckArgumentNull(nameof(left));
Expand Down Expand Up @@ -72,7 +72,7 @@ public static IEnumerable<TResult> RightOuterJoin<TLeft, TRight, TKey, TResult>(
[NotNull] Func<TLeft, TKey> leftKeySelector,
[NotNull] Func<TRight, TKey> rightKeySelector,
[NotNull] Func<TLeft, TRight, TResult> resultSelector,
TLeft defaultLeft = default,
TLeft defaultLeft = default!,
IEqualityComparer<TKey>? keyComparer = null)
{
left.CheckArgumentNull(nameof(left));
Expand Down Expand Up @@ -112,8 +112,8 @@ public static IEnumerable<TResult> FullOuterJoin<TLeft, TRight, TKey, TResult>(
[NotNull] Func<TLeft, TKey> leftKeySelector,
[NotNull] Func<TRight, TKey> rightKeySelector,
[NotNull] Func<TKey, TLeft, TRight, TResult> resultSelector,
TLeft defaultLeft = default,
TRight defaultRight = default,
TLeft defaultLeft = default!,
TRight defaultRight = default!,
IEqualityComparer<TKey>? keyComparer = null)
{
left.CheckArgumentNull(nameof(left));
Expand Down
2 changes: 1 addition & 1 deletion src/Linq.Extras/ToHierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IEnumerable<INode<TSource>> ToHierarchy<TSource, TId>(
[NotNull] this IEnumerable<TSource> source,
[NotNull] Func<TSource, TId> idSelector,
[NotNull] Func<TSource, TId> parentIdSelector,
TId rootParentId = default)
TId rootParentId = default!)
{
source.CheckArgumentNull(nameof(source));
idSelector.CheckArgumentNull(nameof(idSelector));
Expand Down
22 changes: 20 additions & 2 deletions src/Linq.Extras/XComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ public ByKeyComparer(Func<T, TKey> keySelector, IComparer<TKey>? keyComparer)
_keyComparer = keyComparer ?? Comparer<TKey>.Default;
}

#if FEATURE_COMPARER_NULLABILITY
public int Compare(T? x, T? y)
#else
public int Compare(T x, T y)
#endif
{
return _keyComparer.Compare(_keySelector(x), _keySelector(y));
return _keyComparer.Compare(
x is null ? default! : _keySelector(x),
y is null ? default! : _keySelector(y));
}
}

Expand All @@ -197,7 +203,11 @@ public ReverseComparer(IComparer<T> baseComparer)
_baseComparer = baseComparer;
}

#if FEATURE_COMPARER_NULLABILITY
public int Compare(T? x, T? y)
#else
public int Compare(T x, T y)
#endif
{
return _baseComparer.Compare(y, x);
}
Expand All @@ -217,7 +227,11 @@ public ChainedComparer(IEnumerable<IComparer<T>> comparers)
Comparers = comparers.ToArray();
}

#if FEATURE_COMPARER_NULLABILITY
public int Compare(T? x, T? y)
#else
public int Compare(T x, T y)
#endif
{
return Comparers.Select(comparer => comparer.Compare(x, y)).FirstOrDefault(cmp => cmp != 0);
}
Expand All @@ -232,10 +246,14 @@ public DelegateComparer(Comparison<T> comparison)
_comparison = comparison;
}

#if FEATURE_COMPARER_NULLABILITY
public int Compare(T? x, T? y) => _comparison(x!, y!);
#else
public int Compare(T x, T y) => _comparison(x, y);
#endif
}

#endregion
#endregion

}

Expand Down
13 changes: 10 additions & 3 deletions src/Linq.Extras/XEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,25 @@ public ByKeyEqualityComparer(Func<TSource, TKey> keySelector, IEqualityComparer<
_keyComparer = keyComparer ?? EqualityComparer<TKey>.Default;
}

#if FEATURE_COMPARER_NULLABILITY
public bool Equals(TSource? x, TSource? y)
#else
public bool Equals(TSource x, TSource y)
#endif
{
return _keyComparer.Equals(_keySelector(x), _keySelector(y));
return _keyComparer.Equals(
x is null ? default! : _keySelector(x),
y is null ? default! : _keySelector(y));
}

public int GetHashCode(TSource obj)
{
return _keyComparer.GetHashCode(_keySelector(obj));
var key = _keySelector(obj);
return key is null ? 0 : _keyComparer.GetHashCode(key);
}
}

#endregion
#endregion
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions tests/Linq.Extras.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public static void AssertThrowsWhenArgumentNull(Expression<Action> expr)
var parameters = method.GetParameters();
var paramIndexes = parameters
.Select((p, i) => new { p, i })
.ToDictionary(x => x.p.Name, x => x.i);
.ToDictionary(x => x.p.Name!, x => x.i);
var paramTypes = parameters
.ToDictionary(p => p.Name, p => p.ParameterType);
.ToDictionary(p => p.Name!, p => p.ParameterType);

var nonNullableRefParams = parameters
.Where(p => !p.ParameterType.GetTypeInfo().IsValueType && GetNullability(p, defaultNullability) == Nullability.NotNull);

foreach (var param in nonNullableRefParams)
{
var paramName = param.Name;
var paramName = param.Name!;
var args = realArgs.ToArray();
args[paramIndexes[paramName]] = Expression.Constant(null, paramTypes[paramName]);
var call = Expression.Call(realCall.Object, method, args);
Expand Down

0 comments on commit 4a148b7

Please sign in to comment.