#4136: Simplify bool-typed if (c) 1 else 0 to c != 0

An IfInstruction tests its condition for non-zero, so a bool-typed
`if (c) ldc.i4 1 else ldc.i4 0` always equals `c != 0`, which
ExpressionTransforms already folds to `c` for bool `c`. Without this
identity the node reached the C# output as `c ? true : false`, or as
`c || 1 == 0` once negated. The lost `??` form is not addressed here.
This commit is contained in:
dualfroz committed 2026-10-10 13:50:53 +02:00
1 parent 6e2842992c
commit 72e7db13fa
2 files changed
+58

No files matched your search

@@ -584,6 +584,17 @@ namespace ICSharpCode.Decompiler.IL.Transforms
context.EndStep(matchCondition);
return;
}
if (inst.TrueInst.MatchLdcI4(1) && inst.FalseInst.MatchLdcI4(0)
&& inst.InferType(context.TypeSystem).IsKnownType(KnownTypeCode.Boolean))
{
context.Step("if (c) true else false -> comp(c != 0)", inst);
var comp = new Comp(ComparisonKind.Inequality, Sign.None, inst.Condition, new LdcI4(0));
comp.AddILRange(inst);
inst.ReplaceWith(comp);
context.EndStep(comp);
comp.AcceptVisitor(this);
return;
}
}
IfInstruction HandleConditionalOperator(IfInstruction inst)