#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

@@ -935,6 +935,53 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//return (a + 1) ?? (b + 2) ?? (c + 3) ?? (d + 4);
return 0L;
}
#if CS90
private static IDisposable Open(int length)
{
return null;
}
public static void NullOrZeroPatternBeforeUsing(int? num)
{
#if EXPECTED_OUTPUT
if (!num.HasValue || num.GetValueOrDefault() == 0)
#else
if (num is null or 0)
#endif
{
return;
}
using (Open(num.Value))
{
Print(num.Value);
}
}
public static void NullOrZeroPatternEarlyReturn(int? num)
{
#if EXPECTED_OUTPUT
#if OPT && !ROSLYN4
if ((num ?? 0) != 0)
#else
if (num.HasValue && num.GetValueOrDefault() != 0)
#endif
{
Print(num);
Print(num.Value);
Print(num.Value + 1);
}
#else
if (num is null or 0)
{
return;
}
Print(num);
Print(num.Value);
Print(num.Value + 1);
#endif
}
#endif
}
internal class T04_LiftedCompoundAssignAndBoxing