Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ILInspector.Decompiler.Tests/IrImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,20 @@ public void BooleanFolding_TernarySource_PerConfigShape()
#endif
}

[Fact]
public void BooleanFolding_GuardReturn_BothConstantArms_CollapseToCondition()
{
// if (a > 0) { if (b > 0) return true; } return false; folds the nested
// guards to one condition, then the dual-constant return to the bare
// condition — not a dead `a > 0 && b > 0 && true` (the true arm is the
// result, not an extra && term).
using var source = MetadataSource.Open(typeof(CfgSampleClass).Assembly.Location);
string output = PrintWithPasses(
typeof(CfgSampleClass).FullName!, nameof(CfgSampleClass.BothPositive), source);

Assert.Equal("return a > 0 && b > 0;", output);
}

[Fact]
public void Structuring_NestedGuards_NestAndDropGotos()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,15 @@ static bool FoldGuardReturn(IfStatement guard)
var condition = guard.Condition;
condition.Detach();
IrExpression folded;
if (tailConstant is { } tailBool)
if (thenConstant is { } thenBool && tailConstant is { } tailBool2 && thenBool != tailBool2)
{
// Both arms are opposite bool constants, so the condition itself is
// the result: if (c) return true; return false; ≡ return c; and the
// dual ≡ return !c;. The generic fold below would append the literal
// arm as a dead `c && true` / `!c || false`.
folded = thenBool ? condition : Conditions.Negate(condition);
}
else if (tailConstant is { } tailBool)
{
thenValue.Detach();
// if (c) return A; return true; ≡ return !c || A;
Expand Down
Loading