Skip to content

Commit

Permalink
use NotNullWhen
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Aug 20, 2023
1 parent baff3b2 commit eb8377e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Dapper/NRT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if !NET5_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class NotNullWhenAttribute : Attribute
{
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;

public bool ReturnValue { get; }
}
}
#endif
4 changes: 2 additions & 2 deletions Dapper/SqlMapper.DapperRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ internal bool TryGetValue(int index, out object? value)
{
if (index < 0)
{ // doesn't exist
value = null!;
value = null;
return false;
}
// exists, **even if** we don't have a value; consider table rows heterogeneous
value = index < values.Length ? values[index] : null;
if (value is DeadValue)
{ // pretend it isn't here
value = null!;
value = null;
return false;
}
return true;
Expand Down
11 changes: 6 additions & 5 deletions Dapper/SqlMapper.Link.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Dapper
{
Expand All @@ -14,18 +15,18 @@ public static partial class SqlMapper
internal class Link<TKey, TValue> where TKey : class
{
public static void Clear(ref Link<TKey, TValue>? head) => Interlocked.Exchange(ref head, null);
public static bool TryGet(Link<TKey, TValue>? link, TKey key, out TValue value)
public static bool TryGet(Link<TKey, TValue>? link, TKey key, [NotNullWhen(true)] out TValue? value)
{
while (link is not null)
{
if ((object)key == (object)link.Key)
{
value = link.Value;
value = link.Value!;
return true;
}
link = link.Tail;
}
value = default!;
value = default;
return false;
}

Expand All @@ -35,7 +36,7 @@ public static bool TryAdd(ref Link<TKey, TValue>? head, TKey key, ref TValue val
do
{
var snapshot = Interlocked.CompareExchange(ref head, null, null);
if (TryGet(snapshot, key, out TValue found))
if (TryGet(snapshot, key, out TValue? found))
{ // existing match; report the existing value instead
value = found;
return false;
Expand Down
7 changes: 4 additions & 3 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ private static void CollectCacheGarbage()

private const int COLLECT_PER_ITEMS = 1000, COLLECT_HIT_COUNT_MIN = 0;
private static int collect;
private static bool TryGetQueryCache(Identity key, out CacheInfo value)

private static bool TryGetQueryCache(Identity key, [NotNullWhen(true)] out CacheInfo? value)
{
if (_queryCache.TryGetValue(key, out value!))
{
value.RecordHit();
return true;
}
value = null!;
value = null;
return false;
}

Expand Down Expand Up @@ -1817,7 +1818,7 @@ private static int GetNextSplit(int startIdx, string splitOn, DbDataReader reade

private static CacheInfo GetCacheInfo(Identity identity, object? exampleParameters, bool addToCache)
{
if (!TryGetQueryCache(identity, out CacheInfo info))
if (!TryGetQueryCache(identity, out CacheInfo? info))
{
if (GetMultiExec(exampleParameters) is not null)
{
Expand Down

0 comments on commit eb8377e

Please sign in to comment.