Skip to content

Commit 26407ec

Browse files
authored
Merge pull request #800 from hmoratopcs/patch-1
Improve nullability annotations on the Adapt extension method
2 parents 3a92a3d + 0aa9290 commit 26407ec

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/Mapster/TypeAdapter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Reflection;
56
using Mapster.Models;
@@ -24,7 +25,8 @@ public static ITypeAdapterBuilder<TSource> BuildAdapter<TSource>(this TSource so
2425
/// <typeparam name="TDestination">Destination type.</typeparam>
2526
/// <param name="source">Source object to adapt.</param>
2627
/// <returns>Adapted destination type.</returns>
27-
public static TDestination Adapt<TDestination>(this object? source)
28+
[return: NotNullIfNotNull(nameof(source))]
29+
public static TDestination? Adapt<TDestination>(this object? source)
2830
{
2931
return Adapt<TDestination>(source, TypeAdapterConfig.GlobalSettings);
3032
}
@@ -36,14 +38,15 @@ public static TDestination Adapt<TDestination>(this object? source)
3638
/// <param name="source">Source object to adapt.</param>
3739
/// <param name="config">Configuration</param>
3840
/// <returns>Adapted destination type.</returns>
39-
public static TDestination Adapt<TDestination>(this object? source, TypeAdapterConfig config)
41+
[return: NotNullIfNotNull(nameof(source))]
42+
public static TDestination? Adapt<TDestination>(this object? source, TypeAdapterConfig config)
4043
{
4144
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
4245
if (source == null)
43-
return default!;
46+
return default;
4447
var type = source.GetType();
4548
var fn = config.GetDynamicMapFunction<TDestination>(type);
46-
return fn(source);
49+
return fn(source)!;
4750
}
4851

4952
/// <summary>

0 commit comments

Comments
 (0)