From 37ff23f12594e0c866550a6adeac78dc5f637663 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 21 Jun 2026 21:23:23 +0200 Subject: [PATCH] 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 --- ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs | 8 ++++---- .../Syntax/Statements/VariableDeclarationStatement.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs b/ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs index 2b9eeac08..d909c26e8 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/AstNodeCollection.cs @@ -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. /// - public T FirstOrNull(Func? predicate = null) + public T? FirstOrNull(Func? predicate = null) { if (list != null) foreach (T item in list) if (predicate == null || predicate(item)) return item; - return null!; + return null; } /// /// Returns the last element for which the predicate returns true, /// or null if no such object is found. /// - public T LastOrNull(Func? predicate = null) + public T? LastOrNull(Func? predicate = null) { - T result = null!; + T? result = null; if (list != null) foreach (T item in list) if (predicate == null || predicate(item)) diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/VariableDeclarationStatement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/VariableDeclarationStatement.cs index f1ac343e1..8d1425180 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/VariableDeclarationStatement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/VariableDeclarationStatement.cs @@ -48,7 +48,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax [Slot("Variable")] public partial AstNodeCollection Variables { get; } - public VariableInitializer GetVariable(string name) + public VariableInitializer? GetVariable(string name) { return Variables.FirstOrNull(vi => vi.Name == name); }