Skip to content

Commit 277b7ea

Browse files
committed
Added more nullability annotations, minor code tweaks
1 parent 74704ee commit 277b7ea

File tree

8 files changed

+22
-21
lines changed

8 files changed

+22
-21
lines changed

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableRecipient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected virtual void Broadcast<T>(T oldValue, T newValue, string? propertyName
118118
{
119119
PropertyChangedMessage<T> message = new(this, propertyName, oldValue, newValue);
120120

121-
Messenger.Send(message);
121+
_ = Messenger.Send(message);
122122
}
123123

124124
/// <summary>

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static Dictionary<string, string> GetDisplayNames(Type type)
727727

728728
// This method replicates the logic of DisplayName and GetDisplayName from the
729729
// ValidationContext class. See the original source in the BCL for more details.
730-
DisplayNamesMap.GetValue(GetType(), static t => GetDisplayNames(t)).TryGetValue(propertyName, out string? displayName);
730+
_ = DisplayNamesMap.GetValue(GetType(), static t => GetDisplayNames(t)).TryGetValue(propertyName, out string? displayName);
731731

732732
return displayName ?? propertyName;
733733
}

Microsoft.Toolkit.Mvvm/Input/AsyncRelayCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public bool CanExecute(object? parameter)
146146
/// <inheritdoc/>
147147
public void Execute(object? parameter)
148148
{
149-
ExecuteAsync(parameter);
149+
_ = ExecuteAsync(parameter);
150150
}
151151

152152
/// <inheritdoc/>

Microsoft.Toolkit.Mvvm/Input/AsyncRelayCommand{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ public bool CanExecute(object? parameter)
144144
[MethodImpl(MethodImplOptions.AggressiveInlining)]
145145
public void Execute(T? parameter)
146146
{
147-
ExecuteAsync(parameter);
147+
_ = ExecuteAsync(parameter);
148148
}
149149

150150
/// <inheritdoc/>
151151
public void Execute(object? parameter)
152152
{
153-
ExecuteAsync((T?)parameter);
153+
_ = ExecuteAsync((T?)parameter);
154154
}
155155

156156
/// <inheritdoc/>

Microsoft.Toolkit.Mvvm/Messaging/Internals/ConditionalWeakTable2{TKey,TValue}.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System;
88
using System.Collections.Generic;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.Diagnostics.Contracts;
1011
using System.Runtime.CompilerServices;
1112

@@ -33,7 +34,7 @@ internal sealed class ConditionalWeakTable2<TKey, TValue>
3334
private readonly LinkedList<WeakReference<TKey>> keys = new();
3435

3536
/// <inheritdoc cref="ConditionalWeakTable{TKey,TValue}.TryGetValue"/>
36-
public bool TryGetValue(TKey key, out TValue? value)
37+
public bool TryGetValue(TKey key, [NotNullWhen(true)] out TValue? value)
3738
{
3839
return this.table.TryGetValue(key, out value);
3940
}

Microsoft.Toolkit.Mvvm/Messaging/Messages/AsyncCollectionRequestMessage{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task<IReadOnlyCollection<T>> GetResponsesAsync(CancellationToken ca
9999
{
100100
if (cancellationToken.CanBeCanceled)
101101
{
102-
cancellationToken.Register(this.cancellationTokenSource.Cancel);
102+
_ = cancellationToken.Register(this.cancellationTokenSource.Cancel);
103103
}
104104

105105
List<T> results = new(this.responses.Count);
@@ -119,7 +119,7 @@ public async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellati
119119
{
120120
if (cancellationToken.CanBeCanceled)
121121
{
122-
cancellationToken.Register(this.cancellationTokenSource.Cancel);
122+
_ = cancellationToken.Register(this.cancellationTokenSource.Cancel);
123123
}
124124

125125
foreach (var (task, func) in this.responses)

Microsoft.Toolkit.Mvvm/Messaging/StrongReferenceMessenger.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void Register<TRecipient, TMessage, TToken>(TRecipient recipient, TToken
138138

139139
set ??= new HashSet<IMapping>();
140140

141-
set.Add(mapping);
141+
_ = set.Add(mapping);
142142
}
143143
}
144144

@@ -169,12 +169,12 @@ public void UnregisterAll(object recipient)
169169
// dictionary (a hashed collection) only costs O(1) in the best case, while
170170
// if we had tried to iterate the whole dictionary every time we would have
171171
// paid an O(n) minimum cost for each single remove operation.
172-
this.typesMap.TryRemove(mapping.TypeArguments);
172+
_ = this.typesMap.TryRemove(mapping.TypeArguments);
173173
}
174174
}
175175

176176
// Remove the associated set in the recipients map
177-
this.recipientsMap.TryRemove(key);
177+
_ = this.recipientsMap.TryRemove(key);
178178
}
179179
}
180180

@@ -246,7 +246,7 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
246246
holder.Count == 0)
247247
{
248248
// If the map is empty, remove the recipient entirely from its container
249-
map.TryRemove(key);
249+
_ = map.TryRemove(key);
250250

251251
// If no handlers are left at all for the recipient, across all
252252
// message types and token types, remove the set of mappings
@@ -257,7 +257,7 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
257257
set.Remove(Unsafe.As<IMapping>(map)) &&
258258
set.Count == 0)
259259
{
260-
this.recipientsMap.TryRemove(key);
260+
_ = this.recipientsMap.TryRemove(key);
261261
}
262262
}
263263
}
@@ -312,14 +312,14 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
312312
// of the specific token value (ie. the channel used to receive messages of that type).
313313
// We can remove the map entirely from this container, and remove the link to the map itself
314314
// to the current mapping between existing registered recipients (or entire recipients too).
315-
mapping.TryRemove(key);
315+
_ = mapping.TryRemove(key);
316316

317317
HashSet<IMapping> set = this.recipientsMap[key];
318318

319319
if (set.Remove(mapping) &&
320320
set.Count == 0)
321321
{
322-
this.recipientsMap.TryRemove(key);
322+
_ = this.recipientsMap.TryRemove(key);
323323
}
324324
}
325325
}

Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public bool IsRegistered<TMessage, TToken>(object recipient, TToken token)
9898
return
9999
this.recipientsMap.TryGetValue(type2, out RecipientsTable? table) &&
100100
table.TryGetValue(recipient, out IDictionarySlim? mapping) &&
101-
Unsafe.As<DictionarySlim<TToken, object>>(mapping)!.ContainsKey(token);
101+
Unsafe.As<DictionarySlim<TToken, object>>(mapping).ContainsKey(token);
102102
}
103103
}
104104

@@ -145,7 +145,7 @@ public void UnregisterAll(object recipient)
145145
// as that is responsibility of a separate method defined below.
146146
while (enumerator.MoveNext())
147147
{
148-
enumerator.Value.Remove(recipient);
148+
_ = enumerator.Value.Remove(recipient);
149149
}
150150
}
151151
}
@@ -166,7 +166,7 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
166166
if (enumerator.Key.TToken == typeof(TToken) &&
167167
enumerator.Value.TryGetValue(recipient, out IDictionarySlim? mapping))
168168
{
169-
Unsafe.As<DictionarySlim<TToken, object>>(mapping)!.TryRemove(token);
169+
_ = Unsafe.As<DictionarySlim<TToken, object>>(mapping).TryRemove(token);
170170
}
171171
}
172172
}
@@ -186,7 +186,7 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
186186
if (this.recipientsMap.TryGetValue(type2, out RecipientsTable? value) &&
187187
value.TryGetValue(recipient, out IDictionarySlim? mapping))
188188
{
189-
Unsafe.As<DictionarySlim<TToken, object>>(mapping)!.TryRemove(token);
189+
_ = Unsafe.As<DictionarySlim<TToken, object>>(mapping).TryRemove(token);
190190
}
191191
}
192192
}
@@ -334,7 +334,7 @@ private void CleanupWithoutLock()
334334
// Remove the handler maps for recipients that are still alive but with no handlers
335335
foreach (object recipient in emptyRecipients.Span)
336336
{
337-
enumerator.Value.Remove(recipient);
337+
_ = enumerator.Value.Remove(recipient);
338338
}
339339

340340
// Track the type combinations with no recipients or handlers left
@@ -347,7 +347,7 @@ private void CleanupWithoutLock()
347347
// Remove all the mappings with no handlers left
348348
foreach (Type2 key in type2s.Span)
349349
{
350-
this.recipientsMap.TryRemove(key);
350+
_ = this.recipientsMap.TryRemove(key);
351351
}
352352
}
353353

0 commit comments

Comments
 (0)