diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs index f66be670d..2b26f7d3a 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -28,6 +28,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty Item2 } + public enum NoZero + { + Item1 = 1, + Item2 + } + public enum OutOfOrderMembers { Item1 = 1, @@ -135,5 +141,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { return AttributeTargets.Class | AttributeTargets.Delegate; } + + public void EnumInNotZeroCheck(SimpleEnum value, NoZero value2) + { + if (value != SimpleEnum.Item1) + { + Console.WriteLine(); + } + + if (value2 != 0) + { + Console.WriteLine(); + } + } } } diff --git a/ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs b/ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs index ad864b5ac..66d0eab36 100644 --- a/ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs +++ b/ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2014 Daniel Grunwald +// Copyright (c) 2014 Daniel Grunwald // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -731,6 +731,17 @@ namespace ICSharpCode.Decompiler.CSharp .WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual, this.ResolveResult, nullRef.ResolveResult)); } + else if (Type.Kind == TypeKind.Enum && Type.GetDefinition() is { } typeDef && + typeDef.Fields.Any(f => f.GetConstantValue() is { } val && (ulong)CSharpPrimitiveCast.Cast(TypeCode.UInt64, val, false) == 0L)) + { + var zero = expressionBuilder + .ConvertConstantValue(new ConstantResolveResult(Type, 0), allowImplicitConversion: true); + var op = negate ? BinaryOperatorType.Equality : BinaryOperatorType.InEquality; + return new BinaryOperatorExpression(Expression, op, zero.Expression) + .WithoutILInstruction() + .WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual, + this.ResolveResult, zero.ResolveResult)); + } else { var zero = new PrimitiveExpression(0)