Skip to content

Commit 49a9bf2

Browse files
committed
Annotate ObjectPool with nullable
1 parent b5105e7 commit 49a9bf2

10 files changed

+24
-17
lines changed

src/ObjectPool/ref/Microsoft.Extensions.ObjectPool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.Extensions.ObjectPool.netstandard2.0.cs" />

src/ObjectPool/ref/Microsoft.Extensions.ObjectPool.netcoreapp.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public partial class DefaultObjectPool<T> : Microsoft.Extensions.ObjectPool.Obje
1313
{
1414
public DefaultObjectPool(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy) { }
1515
public DefaultObjectPool(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy, int maximumRetained) { }
16+
[return: System.Diagnostics.CodeAnalysis.NotNullAttribute]
1617
public override T Get() { throw null; }
1718
public override void Return(T obj) { }
1819
}
@@ -40,7 +41,7 @@ public override void Return(T obj) { }
4041
}
4142
public static partial class ObjectPool
4243
{
43-
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy = null) where T : class, new() { throw null; }
44+
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>? policy = null) where T : class, new() { throw null; }
4445
}
4546
public abstract partial class ObjectPoolProvider
4647
{
@@ -56,6 +57,7 @@ public static partial class ObjectPoolProviderExtensions
5657
public abstract partial class ObjectPool<T> where T : class
5758
{
5859
protected ObjectPool() { }
60+
[return: System.Diagnostics.CodeAnalysis.NotNullAttribute]
5961
public abstract T Get();
6062
public abstract void Return(T obj);
6163
}

src/ObjectPool/ref/Microsoft.Extensions.ObjectPool.netstandard2.0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void Return(T obj) { }
4040
}
4141
public static partial class ObjectPool
4242
{
43-
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy = null) where T : class, new() { throw null; }
43+
public static Microsoft.Extensions.ObjectPool.ObjectPool<T> Create<T>(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T>? policy = null) where T : class, new() { throw null; }
4444
}
4545
public abstract partial class ObjectPoolProvider
4646
{

src/ObjectPool/src/DefaultObjectPool.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using System.Threading;
89

@@ -18,10 +19,10 @@ public class DefaultObjectPool<T> : ObjectPool<T> where T : class
1819
private protected readonly ObjectWrapper[] _items;
1920
private protected readonly IPooledObjectPolicy<T> _policy;
2021
private protected readonly bool _isDefaultPolicy;
21-
private protected T _firstItem;
22+
private protected T? _firstItem;
2223

2324
// This class was introduced in 2.1 to avoid the interface call where possible
24-
private protected readonly PooledObjectPolicy<T> _fastPolicy;
25+
private protected readonly PooledObjectPolicy<T>? _fastPolicy;
2526

2627
/// <summary>
2728
/// Creates an instance of <see cref="DefaultObjectPool{T}"/>.
@@ -54,6 +55,7 @@ bool IsDefaultPolicy()
5455
}
5556
}
5657

58+
[return: NotNull]
5759
public override T Get()
5860
{
5961
var item = _firstItem;
@@ -97,7 +99,7 @@ public override void Return(T obj)
9799
[DebuggerDisplay("{Element}")]
98100
private protected struct ObjectWrapper
99101
{
100-
public T Element;
102+
public T? Element;
101103
}
102104
}
103105
}

src/ObjectPool/src/DisposableObjectPool.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Runtime.CompilerServices;
65
using System.Threading;
76

87
namespace Microsoft.Extensions.ObjectPool
@@ -82,7 +81,7 @@ public void Dispose()
8281
}
8382
}
8483

85-
private void DisposeItem(T item)
84+
private void DisposeItem(T? item)
8685
{
8786
if (item is IDisposable disposable)
8887
{

src/ObjectPool/src/LeakTrackingObjectPool.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -31,8 +31,7 @@ public override T Get()
3131

3232
public override void Return(T obj)
3333
{
34-
Tracker tracker;
35-
if (_trackers.TryGetValue(obj, out tracker))
34+
if (_trackers.TryGetValue(obj, out var tracker))
3635
{
3736
_trackers.Remove(obj);
3837
tracker.Dispose();

src/ObjectPool/src/Microsoft.Extensions.ObjectPool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageTags>pooling</PackageTags>
99
<IsPackable>true</IsPackable>
1010
<IsAspNetCoreApp>true</IsAspNetCoreApp>
11+
<Nullable>enable</Nullable>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/ObjectPool/src/ObjectPool.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.Extensions.ObjectPool
57
{
68
/// <summary>
@@ -28,7 +30,7 @@ public abstract class ObjectPool<T> where T : class
2830
public static class ObjectPool
2931
{
3032
/// <inheritdoc />
31-
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy = null) where T : class, new()
33+
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T>? policy = null) where T : class, new()
3234
{
3335
var provider = new DefaultObjectPoolProvider();
3436
return provider.Create(policy ?? new DefaultPooledObjectPolicy<T>());

src/ObjectPool/test/Microsoft.Extensions.ObjectPool.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework);net472</TargetFrameworks>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67

78
<ItemGroup>

src/ObjectPool/test/ThreadingTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Threading;
@@ -8,8 +8,8 @@ namespace Microsoft.Extensions.ObjectPool
88
{
99
public class ThreadingTest
1010
{
11-
private CancellationTokenSource _cts;
12-
private DefaultObjectPool<Item> _pool;
11+
private CancellationTokenSource _cts = default!;
12+
private DefaultObjectPool<Item> _pool = default!;
1313
private bool _foundError;
1414

1515
[Fact]

0 commit comments

Comments
 (0)