Skip to content

Commit 3aecf6d

Browse files
Add [NotNullIfNotNull] to EnumerableExtensions.Random<T> for better nullable flow analysis
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent fed61ac commit 3aecf6d

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ test
7878
- **`Math.Clamp`**: Use instead of separate `Math.Min`/`Math.Max` calls
7979
- **Generic constraints**: Use `where T : struct, Enum` instead of runtime `typeof(T).IsEnum` checks
8080
- **Pattern matching**: Use `is null` / `is not null` instead of `== null` / `!= null`
81+
- **Nullable flow attributes**: Use `[NotNullIfNotNull]`, `[NotNullWhen]`, `[MaybeNullWhen]`, `[AllowNull]` from `System.Diagnostics.CodeAnalysis` to express conditional nullability contracts that the compiler can track through call sites — prefer these over `T?` return types when nullability depends on a specific argument
8182

8283
### Exceptions
8384

src/Exceptionless.RandomData/RandomData.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using System.Security.Cryptography;
23
using System.Text;
34

@@ -380,7 +381,9 @@ public static string GetParagraphs(int count = 3, int minSentences = 3, int maxS
380381
public static class EnumerableExtensions {
381382
/// <summary>
382383
/// Returns a random element from <paramref name="items"/>, or <paramref name="defaultValue"/> if the sequence is null or empty.
384+
/// The return value is non-null when <paramref name="defaultValue"/> is non-null.
383385
/// </summary>
386+
[return: NotNullIfNotNull(nameof(defaultValue))]
384387
public static T? Random<T>(this IEnumerable<T>? items, T? defaultValue = default) {
385388
if (items is null)
386389
return defaultValue;

0 commit comments

Comments
 (0)