Skip to content

Commit 12f66e9

Browse files
committed
Fixed nullability annotations in Microsoft.Toolkit
1 parent e3d533a commit 12f66e9

39 files changed

+156
-83
lines changed

Microsoft.Toolkit/Attributes/DoesNotReturnAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Diagnostics.CodeAnalysis
99
/// <summary>
1010
/// Applied to a method that will never return under any circumstance.
1111
/// </summary>
12-
/// <remarks>Internal copy of the .NET Standard 2.1 attribute.</remarks>
12+
/// <remarks>Internal copy from the BCL attribute.</remarks>
1313
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
1414
internal sealed class DoesNotReturnAttribute : Attribute
1515
{

Microsoft.Toolkit/Attributes/DoesNotReturnIfAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Diagnostics.CodeAnalysis
1010
/// Specifies that a given <see cref="ParameterValue"/> also indicates
1111
/// whether the method will not return (eg. throw an exception).
1212
/// </summary>
13-
/// <remarks>Internal copy of the .NET Standard 2.1 attribute.</remarks>
13+
/// <remarks>Internal copy from the BCL attribute.</remarks>
1414
[AttributeUsage(AttributeTargets.Parameter)]
1515
internal sealed class DoesNotReturnIfAttribute : Attribute
1616
{

Microsoft.Toolkit/Attributes/MaybeNullAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace System.Diagnostics.CodeAnalysis
99
/// <summary>
1010
/// Specifies that an output may be <see langword="null"/> even if the corresponding type disallows it.
1111
/// </summary>
12+
/// <remarks>Internal copy from the BCL attribute.</remarks>
1213
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
1314
internal sealed class MaybeNullAttribute : Attribute
1415
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if !NETSTANDARD2_1_OR_GREATER
6+
7+
namespace System.Diagnostics.CodeAnalysis
8+
{
9+
/// <summary>
10+
/// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.
11+
/// </summary>
12+
/// <remarks>Internal copy from the BCL attribute.</remarks>
13+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
14+
internal sealed class MaybeNullWhenAttribute : Attribute
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="MaybeNullWhenAttribute"/> class.
18+
/// </summary>
19+
/// <param name="returnValue">The return value condition. If the method returns this value, the associated parameter may be null.</param>
20+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
21+
22+
/// <summary>
23+
/// Gets a value indicating whether the value may be <see langword="null"/> depending on the return value.
24+
/// </summary>
25+
public bool ReturnValue { get; }
26+
}
27+
}
28+
29+
#endif

Microsoft.Toolkit/Attributes/NotNullAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Diagnostics.CodeAnalysis
1010
/// Specifies that an output will not be <see langword="null"/> even if the corresponding type allows it.
1111
/// Specifies that an input argument was not <see langword="null"/> when the call returns.
1212
/// </summary>
13-
/// <remarks>Internal copy of the .NET Standard 2.1 attribute.</remarks>
13+
/// <remarks>Internal copy from the BCL attribute.</remarks>
1414
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
1515
internal sealed class NotNullAttribute : Attribute
1616
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if !NETSTANDARD2_1_OR_GREATER
6+
7+
namespace System.Diagnostics.CodeAnalysis
8+
{
9+
/// <summary>
10+
/// Specifies that the output will be non-null if the named parameter is non-null.
11+
/// </summary>
12+
/// <remarks>Internal copy from the BCL attribute.</remarks>
13+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
14+
internal sealed class NotNullIfNotNullAttribute : Attribute
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="NotNullIfNotNullAttribute"/> class.
18+
/// </summary>
19+
/// <param name="parameterName">The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.</param>
20+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
21+
22+
/// <summary>
23+
/// Gets the associated parameter name.
24+
/// </summary>
25+
public string ParameterName { get; }
26+
}
27+
}
28+
29+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if !NETSTANDARD2_1_OR_GREATER
6+
7+
namespace System.Diagnostics.CodeAnalysis
8+
{
9+
/// <summary>
10+
/// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.
11+
/// </summary>
12+
/// <remarks>Internal copy from the BCL attribute.</remarks>
13+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
14+
internal sealed class NotNullWhenAttribute : Attribute
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="NotNullWhenAttribute"/> class.
18+
/// </summary>
19+
/// <param name="returnValue">The return value condition. If the method returns this value, the associated parameter will not be null.</param>
20+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
21+
22+
/// <summary>
23+
/// Gets a value indicating whether the annotated variable is not <see langword="null"/>.
24+
/// </summary>
25+
public bool ReturnValue { get; }
26+
}
27+
}
28+
29+
#endif

Microsoft.Toolkit/Attributes/SkipLocalsInitAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Runtime.CompilerServices
99
/// <summary>
1010
/// Used to indicate to the compiler that the <c>.locals init</c> flag should not be set in method headers.
1111
/// </summary>
12-
/// <remarks>Internal copy of the .NET 5 attribute.</remarks>
12+
/// <remarks>Internal copy from the BCL attribute.</remarks>
1313
[AttributeUsage(
1414
AttributeTargets.Module |
1515
AttributeTargets.Class |

Microsoft.Toolkit/Collections/ObservableGroup.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Toolkit.Collections
1818
/// <typeparam name="TValue">The type of the items in the collection.</typeparam>
1919
[DebuggerDisplay("Key = {Key}, Count = {Count}")]
2020
public class ObservableGroup<TKey, TValue> : ObservableCollection<TValue>, IGrouping<TKey, TValue>, IReadOnlyObservableGroup
21+
where TKey : notnull
2122
{
2223
/// <summary>
2324
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="Key"/>
@@ -30,7 +31,7 @@ public class ObservableGroup<TKey, TValue> : ObservableCollection<TValue>, IGrou
3031
/// <param name="key">The key for the group.</param>
3132
public ObservableGroup(TKey key)
3233
{
33-
Key = key;
34+
this.key = key;
3435
}
3536

3637
/// <summary>
@@ -40,7 +41,7 @@ public ObservableGroup(TKey key)
4041
public ObservableGroup(IGrouping<TKey, TValue> grouping)
4142
: base(grouping)
4243
{
43-
Key = grouping.Key;
44+
this.key = grouping.Key;
4445
}
4546

4647
/// <summary>
@@ -51,7 +52,7 @@ public ObservableGroup(IGrouping<TKey, TValue> grouping)
5152
public ObservableGroup(TKey key, IEnumerable<TValue> collection)
5253
: base(collection)
5354
{
54-
Key = key;
55+
this.key = key;
5556
}
5657

5758
private TKey key;
@@ -64,7 +65,7 @@ public TKey Key
6465
get => this.key;
6566
set
6667
{
67-
if (!EqualityComparer<TKey>.Default.Equals(this.key, value))
68+
if (!EqualityComparer<TKey>.Default.Equals(this.key!, value))
6869
{
6970
this.key = value;
7071

Microsoft.Toolkit/Collections/ObservableGroupedCollection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Linq;
89
using System.Runtime.CompilerServices;
910

10-
#nullable enable
11-
1211
namespace Microsoft.Toolkit.Collections
1312
{
1413
/// <summary>
@@ -17,6 +16,7 @@ namespace Microsoft.Toolkit.Collections
1716
/// <typeparam name="TKey">The type of the group key.</typeparam>
1817
/// <typeparam name="TValue">The type of the items in the collection.</typeparam>
1918
public sealed class ObservableGroupedCollection<TKey, TValue> : ObservableCollection<ObservableGroup<TKey, TValue>>
19+
where TKey : notnull
2020
{
2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="ObservableGroupedCollection{TKey, TValue}"/> class.
@@ -30,7 +30,7 @@ public ObservableGroupedCollection()
3030
/// </summary>
3131
/// <param name="collection">The initial data to add in the grouped collection.</param>
3232
public ObservableGroupedCollection(IEnumerable<IGrouping<TKey, TValue>> collection)
33-
: base(collection.Select(c => new ObservableGroup<TKey, TValue>(c)))
33+
: base(collection.Select(static c => new ObservableGroup<TKey, TValue>(c)))
3434
{
3535
}
3636

@@ -40,7 +40,7 @@ public ObservableGroupedCollection(IEnumerable<IGrouping<TKey, TValue>> collecti
4040
/// <param name="list">The resulting <see cref="List{T}"/>, if one was in use.</param>
4141
/// <returns>Whether or not a <see cref="List{T}"/> instance has been found.</returns>
4242
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43-
internal bool TryGetList(out List<ObservableGroup<TKey, TValue>>? list)
43+
internal bool TryGetList([NotNullWhen(true)] out List<ObservableGroup<TKey, TValue>>? list)
4444
{
4545
list = Items as List<ObservableGroup<TKey, TValue>>;
4646

0 commit comments

Comments
 (0)