Browse Source

Type FirstOrNull/LastOrNull as nullable instead of suppressing

These helpers are documented to return null when nothing matches, but
returned `null!`, hiding the very nullable warnings #nullable enable is
meant to surface: a miss handed back null typed as non-null and would
NRE downstream with no compile-time signal. Returning T? lets the
compiler enforce the guard at each call site (both existing callers
already null-check). GetVariable forwards the result, so it becomes
VariableInitializer? to match.

Assisted-by: Claude:claude-opus-4-8:Claude Code
pull/3807/head
Siegfried Pammer 2 weeks ago committed by Siegfried Pammer
parent
commit
37ff23f125
  1. 8
      ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs
  2. 2
      ICSharpCode.Decompiler/CSharp/Syntax/Statements/VariableDeclarationStatement.cs

8
ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs

@ -294,22 +294,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -294,22 +294,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
/// Returns the first element for which the predicate returns true,
/// or null if no such object is found.
/// </summary>
public T FirstOrNull(Func<T, bool>? predicate = null)
public T? FirstOrNull(Func<T, bool>? predicate = null)
{
if (list != null)
foreach (T item in list)
if (predicate == null || predicate(item))
return item;
return null!;
return null;
}
/// <summary>
/// Returns the last element for which the predicate returns true,
/// or null if no such object is found.
/// </summary>
public T LastOrNull(Func<T, bool>? predicate = null)
public T? LastOrNull(Func<T, bool>? predicate = null)
{
T result = null!;
T? result = null;
if (list != null)
foreach (T item in list)
if (predicate == null || predicate(item))

2
ICSharpCode.Decompiler/CSharp/Syntax/Statements/VariableDeclarationStatement.cs

@ -48,7 +48,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -48,7 +48,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
[Slot("Variable")]
public partial AstNodeCollection<VariableInitializer> Variables { get; }
public VariableInitializer GetVariable(string name)
public VariableInitializer? GetVariable(string name)
{
return Variables.FirstOrNull(vi => vi.Name == name);
}

Loading…
Cancel
Save