Skip to content

Commit c075f7b

Browse files
Fix out var for expressions appearing in a lambda and not in a statement.
1 parent d11c407 commit c075f7b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,14 @@ public void M4()
7272
GetObject(out dynamic obj);
7373
obj.Method();
7474
}
75+
76+
public void M5()
77+
{
78+
Func<bool> func = () => TryGet<object>(out var result) && result != null;
79+
Func<bool> func2 = () => TryGet<object>(out var result) && result != null;
80+
81+
func();
82+
func2();
83+
}
7584
}
7685
}

ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,11 +776,13 @@ private bool CanBeDeclaredAsOutVariable(VariableToDeclare v, out DirectionExpres
776776
}
777777
switch (node)
778778
{
779-
case IfElseStatement _: // variable declared in if condition appears in parent scope
780-
case ExpressionStatement _:
779+
case IfElseStatement: // variable declared in if condition appears in parent scope
780+
case ExpressionStatement:
781781
return node == v.InsertionPoint.nextNode;
782-
case Statement _:
782+
case Statement:
783783
return false; // other statements (e.g. while) don't allow variables to be promoted to parent scope
784+
case LambdaExpression lambda:
785+
return lambda.Body == v.InsertionPoint.nextNode;
784786
}
785787
}
786788
return false;

0 commit comments

Comments
 (0)