Skip to content

Commit 4ef3581

Browse files
Merge pull request #3425 from ElektroKill/fix/issue3423
Improve generation of not equals check
2 parents 96caa4e + 16600fa commit 4ef3581

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
1+
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
44
// software and associated documentation files (the "Software"), to deal in the Software
@@ -28,6 +28,12 @@ public enum SimpleEnum
2828
Item2
2929
}
3030

31+
public enum NoZero
32+
{
33+
Item1 = 1,
34+
Item2
35+
}
36+
3137
public enum OutOfOrderMembers
3238
{
3339
Item1 = 1,
@@ -135,5 +141,18 @@ public object PreservingTypeWhenBoxedTwoEnum()
135141
{
136142
return AttributeTargets.Class | AttributeTargets.Delegate;
137143
}
144+
145+
public void EnumInNotZeroCheck(SimpleEnum value, NoZero value2)
146+
{
147+
if (value != SimpleEnum.Item1)
148+
{
149+
Console.WriteLine();
150+
}
151+
152+
if (value2 != 0)
153+
{
154+
Console.WriteLine();
155+
}
156+
}
138157
}
139158
}

ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014 Daniel Grunwald
1+
// Copyright (c) 2014 Daniel Grunwald
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
44
// software and associated documentation files (the "Software"), to deal in the Software
@@ -731,6 +731,17 @@ public TranslatedExpression ConvertToBoolean(ExpressionBuilder expressionBuilder
731731
.WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual,
732732
this.ResolveResult, nullRef.ResolveResult));
733733
}
734+
else if (Type.Kind == TypeKind.Enum && Type.GetDefinition() is { } typeDef &&
735+
typeDef.Fields.Any(f => f.GetConstantValue() is { } val && (ulong)CSharpPrimitiveCast.Cast(TypeCode.UInt64, val, false) == 0L))
736+
{
737+
var zero = expressionBuilder
738+
.ConvertConstantValue(new ConstantResolveResult(Type, 0), allowImplicitConversion: true);
739+
var op = negate ? BinaryOperatorType.Equality : BinaryOperatorType.InEquality;
740+
return new BinaryOperatorExpression(Expression, op, zero.Expression)
741+
.WithoutILInstruction()
742+
.WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual,
743+
this.ResolveResult, zero.ResolveResult));
744+
}
734745
else
735746
{
736747
var zero = new PrimitiveExpression(0)

0 commit comments

Comments
 (0)