Skip to content

Commit d91ef4e

Browse files
Nullable Reference Types
1 parent 057b393 commit d91ef4e

15 files changed

Lines changed: 95 additions & 48 deletions

src/ExpressionShortcuts.Tests/ExpressionShortcuts.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp1.1;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp1.1;netcoreapp3.1;net8.0;net10.0</TargetFrameworks>
55
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net45;net451;net452</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
<RootNamespace>Expressions.Shortcuts.Tests</RootNamespace>
88
<LangVersion>latest</LangVersion>
99
<ProjectGuid>6F69915C-EF93-4693-A8F8-EA74FA4A3BD3</ProjectGuid>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/ExpressionShortcuts.Tests/IntegrationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public void NestedCallWithPropertyTest()
119119
Expression.Assign(mockVariable, Expression.Constant(_mock)),
120120
Expression.Call(
121121
mockVariable,
122-
typeof(IMock).GetMethod(nameof(IMock.VoidMethodWithParameter)),
123-
Expression.Property(mockVariable, typeof(IMock).GetProperty(nameof(IMock.String)))
122+
typeof(IMock).GetMethod(nameof(IMock.VoidMethodWithParameter))!,
123+
Expression.Property(mockVariable, typeof(IMock).GetProperty(nameof(IMock.String))!)
124124
)
125125
);
126126

src/ExpressionShortcuts/BlockBuilder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Linq.Expressions;
@@ -10,11 +11,11 @@ namespace Expressions.Shortcuts
1011
/// </summary>
1112
internal class BlockBuilder: ExpressionContainer
1213
{
13-
private readonly Type _returnType;
14+
private readonly Type? _returnType;
1415
private readonly List<Expression> _expressions;
1516
private readonly HashSet<ParameterExpression> _parameters;
1617

17-
internal BlockBuilder(Type returnType) : base(Expression.Empty())
18+
internal BlockBuilder(Type? returnType) : base(Expression.Empty())
1819
{
1920
_returnType = returnType;
2021
_expressions = new List<Expression>();

src/ExpressionShortcuts/ConditionBuilder.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Linq.Expressions;
34

45
namespace Expressions.Shortcuts
@@ -8,12 +9,12 @@ namespace Expressions.Shortcuts
89
/// </summary>
910
internal class ConditionBuilder : ExpressionContainer
1011
{
11-
private readonly Type _type;
12-
private Expression _condition;
13-
private Expression _then;
14-
private Expression _else;
12+
private readonly Type? _type;
13+
private Expression? _condition;
14+
private Expression? _then;
15+
private Expression? _else;
1516

16-
internal ConditionBuilder(Type type) : base(Expression.Empty())
17+
internal ConditionBuilder(Type? type) : base(Expression.Empty())
1718
{
1819
_type = type;
1920
}
@@ -145,6 +146,7 @@ public override Expression Expression
145146
get
146147
{
147148
if(_condition == null) throw new InvalidOperationException("`if` statement is not defined");
149+
if(_then == null) throw new InvalidOperationException("`then` statement is not defined");
148150

149151
return _else == null
150152
? Expression.IfThen(_condition, _then)

src/ExpressionShortcuts/ExpressionContainer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Linq.Expressions;
34

45
namespace Expressions.Shortcuts
@@ -92,7 +93,7 @@ internal class ExpressionContainer<T> : ExpressionContainer
9293
/// <summary>
9394
/// Used to trick C# compiler
9495
/// </summary>
95-
public static implicit operator T(ExpressionContainer<T> _0) => default(T);
96+
public static implicit operator T(ExpressionContainer<T> _0) => default(T)!;
9697

9798
/// <summary>
9899
///

src/ExpressionShortcuts/ExpressionContainerExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq.Expressions;
45
using System.Runtime.CompilerServices;

src/ExpressionShortcuts/ExpressionExtractorVisitor.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using System;
1+
#nullable enable
2+
using System;
3+
using System.Diagnostics.CodeAnalysis;
24
using System.Linq.Expressions;
35
using System.Reflection;
46

57
namespace Expressions.Shortcuts
68
{
79
internal class ExpressionExtractorVisitor : ExpressionVisitor
810
{
9-
public override Expression Visit(Expression node)
11+
[return: NotNullIfNotNull("node")]
12+
public override Expression? Visit(Expression? node)
1013
{
1114
switch (node)
1215
{
@@ -70,7 +73,8 @@ protected override Expression VisitUnary(UnaryExpression node)
7073
}
7174
}
7275

73-
private static Expression ConvertToExpression(object value, Func<Expression, Expression> visit = null)
76+
[return: NotNullIfNotNull("visit")]
77+
private static Expression? ConvertToExpression(object? value, Func<Expression, Expression>? visit = null)
7478
{
7579
if (value is ExpressionContainer expressionContainer) return expressionContainer.Expression;
7680
if (value is Expression expression) return visit?.Invoke(expression);

src/ExpressionShortcuts/ExpressionShortcuts.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq.Expressions;
45
using System.Reflection;
@@ -19,7 +20,7 @@ internal static partial class ExpressionShortcuts
1920
/// <typeparam name="T">Expected type of resulting <see cref="Expression"/></typeparam>
2021
/// <returns><see cref="ExpressionContainer{T}"/></returns>
2122
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
public static ExpressionContainer<T> Arg<T>(Expression expression) => expression == null ? Null<T>() : new ExpressionContainer<T>(expression);
23+
public static ExpressionContainer<T> Arg<T>(Expression? expression) => expression == null ? Null<T>() : new ExpressionContainer<T>(expression);
2324

2425
/// <summary>
2526
/// Creates strongly typed representation of the <see cref="ExpressionContainer.Expression"/>
@@ -39,7 +40,7 @@ internal static partial class ExpressionShortcuts
3940
/// <typeparam name="T">Expected type of resulting <see cref="Expression"/></typeparam>
4041
/// <returns><see cref="ExpressionContainer{T}"/></returns>
4142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
42-
public static ExpressionContainer<T> Arg<T>(Expression<T> expression) => expression == null ? Null<T>() : new ExpressionContainer<T>(expression);
43+
public static ExpressionContainer<T> Arg<T>(Expression<T>? expression) => expression == null ? Null<T>() : new ExpressionContainer<T>(expression);
4344

4445
/// <summary>
4546
/// Creates strongly typed representation of the <paramref name="expression"/> and performs <see cref="Expression.Convert(System.Linq.Expressions.Expression,System.Type)"/> on it.
@@ -49,7 +50,7 @@ internal static partial class ExpressionShortcuts
4950
/// <typeparam name="T">Expected type of resulting <see cref="Expression"/></typeparam>
5051
/// <returns><see cref="ExpressionContainer{T}"/></returns>
5152
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52-
public static ExpressionContainer<T> Cast<T>(Expression expression) => expression == null ? Null<T>() : new ExpressionContainer<T>(Expression.Convert(expression, typeof(T)));
53+
public static ExpressionContainer<T> Cast<T>(Expression? expression) => expression == null ? Null<T>() : new ExpressionContainer<T>(Expression.Convert(expression, typeof(T)));
5354

5455
/// <summary>
5556
/// Creates strongly typed representation of the <see cref="Expression.Variable(System.Type, System.String)"/>
@@ -58,7 +59,7 @@ internal static partial class ExpressionShortcuts
5859
/// <typeparam name="T">Expected type of resulting <see cref="ParameterExpression"/></typeparam>
5960
/// <returns><see cref="ExpressionContainer{T}"/></returns>
6061
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61-
public static ExpressionContainer<T> Var<T>(string name = null)
62+
public static ExpressionContainer<T> Var<T>(string? name = null)
6263
{
6364
return new ExpressionContainer<T>(Expression.Variable(typeof(T), name ?? typeof(T).Name));
6465
}
@@ -70,7 +71,7 @@ public static ExpressionContainer<T> Var<T>(string name = null)
7071
/// <typeparam name="T">Expected type of resulting <see cref="ParameterExpression"/></typeparam>
7172
/// <returns><see cref="ExpressionContainer{T}"/></returns>
7273
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73-
public static ExpressionContainer<T> Parameter<T>(string name = null)
74+
public static ExpressionContainer<T> Parameter<T>(string? name = null)
7475
{
7576
return new ExpressionContainer<T>(Expression.Parameter(typeof(T), name ?? typeof(T).Name));
7677
}
@@ -211,14 +212,14 @@ public static ExpressionContainer<T> New<T>(Expression<Func<T>> invocationExpres
211212
[MethodImpl(MethodImplOptions.AggressiveInlining)]
212213
public static ExpressionContainer<T> New<T>() where T: new()
213214
{
214-
return Arg<T>(Expression.New(typeof(T).GetConstructor(new Type[0])));
215+
return Arg<T>(Expression.New(typeof(T).GetConstructor(Type.EmptyTypes)!));
215216
}
216217

217218
/// <summary>
218219
/// Provides fluent interface for <see cref="BlockExpression"/> creation
219220
/// </summary>
220221
[MethodImpl(MethodImplOptions.AggressiveInlining)]
221-
public static BlockBuilder Block(Type returnType = null)
222+
public static BlockBuilder Block(Type? returnType = null)
222223
{
223224
return new BlockBuilder(returnType);
224225
}
@@ -294,7 +295,7 @@ public static SwitchBuilder<T> Switch<T>(ExpressionContainer<T> value)
294295
/// </summary>
295296
/// <returns></returns>
296297
[MethodImpl(MethodImplOptions.AggressiveInlining)]
297-
public static ConditionBuilder Condition(Type resultType = null)
298+
public static ConditionBuilder Condition(Type? resultType = null)
298299
{
299300
return new ConditionBuilder(resultType);
300301
}

src/ExpressionShortcuts/ExpressionShortcuts.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<PropertyGroup>
44
<DebugType>portable</DebugType>
5-
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
5+
<TargetFrameworks>netstandard1.3;netstandard2.0;netstandard2.1</TargetFrameworks>
66
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net45;net451;net452</TargetFrameworks>
77
<RootNamespace>Expressions.Shortcuts</RootNamespace>
88
<ProjectGuid>B056DA03-44B9-4903-8534-E8F14553C915</ProjectGuid>
9-
<LangVersion>7</LangVersion>
9+
<LangVersion>8</LangVersion>
1010
</PropertyGroup>
1111

1212
<PropertyGroup>
@@ -60,6 +60,11 @@
6060
<Compile Include="$(TargetFramework)\**" />
6161
</ItemGroup>
6262

63+
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.1'">
64+
<Compile Include="netstandard2.0\**" />
65+
<Compile Remove="before-netstandard2.1\**" />
66+
</ItemGroup>
67+
6368
<ItemGroup>
6469
<EmbeddedResource Remove="build\**" />
6570
<EmbeddedResource Remove="buildMultiTargeting\**" />

src/ExpressionShortcuts/ExpressionUtils.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Linq.Expressions;
@@ -32,10 +33,10 @@ IEnumerable<Expression> PerformReplacement()
3233
/// Visits <paramref name="expression"/> and replaces <see cref="ParameterExpression"/> by <paramref name="newValues"/> performing match by <see cref="Expression.Type"/>
3334
/// </summary>
3435
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35-
internal static Expression ReplaceParameters(Expression expression, params Expression[] newValues)
36+
internal static Expression ReplaceParameters(Expression expression, params Expression?[] newValues)
3637
{
3738
var visitor = new ParameterReplacerVisitor(newValues);
38-
return visitor.Visit(expression);
39+
return visitor.Visit(expression)!;
3940
}
4041

4142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -72,12 +73,12 @@ internal static Expression ProcessMemberLambda(Expression instance, LambdaExpres
7273
}
7374

7475
[MethodImpl(MethodImplOptions.AggressiveInlining)]
75-
internal static Expression ProcessCallLambda(LambdaExpression propertyLambda, Expression instance = null)
76+
internal static Expression ProcessCallLambda(LambdaExpression propertyLambda, Expression? instance = null)
7677
{
7778
return ProcessCall(propertyLambda.Body, instance);
7879
}
7980

80-
internal static Expression ProcessCall(Expression propertyLambda, Expression instance = null)
81+
internal static Expression ProcessCall(Expression propertyLambda, Expression? instance = null)
8182
{
8283
switch (propertyLambda)
8384
{

0 commit comments

Comments
 (0)