From c075f7b9c80a5ca85be6d5c36ffb2f044dab50d1 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 11 Oct 2025 21:47:11 +0200 Subject: [PATCH] Fix out var for expressions appearing in a lambda and not in a statement. --- .../TestCases/Pretty/OutVariables.cs | 9 +++++++++ .../CSharp/Transforms/DeclareVariables.cs | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs index d7bd852c3..6d519c523 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs @@ -72,5 +72,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty GetObject(out dynamic obj); obj.Method(); } + + public void M5() + { + Func func = () => TryGet(out var result) && result != null; + Func func2 = () => TryGet(out var result) && result != null; + + func(); + func2(); + } } } diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs b/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs index d64137b80..73f11bc6a 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs @@ -776,11 +776,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms } switch (node) { - case IfElseStatement _: // variable declared in if condition appears in parent scope - case ExpressionStatement _: + case IfElseStatement: // variable declared in if condition appears in parent scope + case ExpressionStatement: return node == v.InsertionPoint.nextNode; - case Statement _: + case Statement: return false; // other statements (e.g. while) don't allow variables to be promoted to parent scope + case LambdaExpression lambda: + return lambda.Body == v.InsertionPoint.nextNode; } } return false;