From 82db34d7cd646c469dfc172b1fb3976097410a59 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 13 Jan 2025 20:04:58 +0100 Subject: [PATCH] Fix #3322: Add missing checks for equality comparison --- .../ICSharpCode.Decompiler.Tests.csproj | 1 + .../PrettyTestRunner.cs | 6 ++++++ .../TestCases/Pretty/Comparisons.cs | 18 ++++++++++++++++++ .../CSharp/Resolver/CSharpResolver.cs | 6 +++++- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index 3f60f45482..11d60fe3e0 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -130,6 +130,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index 284b51401b..f4ff7c9149 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -586,6 +586,12 @@ public async Task OptionalArguments([ValueSource(nameof(defaultOptions))] Compil await RunForLibrary(cscOptions: cscOptions); } + [Test] + public async Task Comparisons([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) + { + await RunForLibrary(cscOptions: cscOptions); + } + [Test] public async Task ConstantsTests([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs new file mode 100644 index 0000000000..5a1df3c9b2 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs @@ -0,0 +1,18 @@ +namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty +{ + public class Comparisons + { + private class A + { + } + + private class B + { + } + + private bool CompareUnrelatedNeedsCast(A a, B b) + { + return (object)a == b; + } + } +} diff --git a/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs b/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs index 14ab787ba0..5d86879cc8 100644 --- a/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs +++ b/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs @@ -805,7 +805,11 @@ public ResolveResult ResolveBinaryOperator(BinaryOperatorType op, ResolveResult } if (op == BinaryOperatorType.Equality || op == BinaryOperatorType.InEquality) { - if (lhsType.IsReferenceType == true && rhsType.IsReferenceType == true) + if (lhsType.IsReferenceType == true && rhsType.IsReferenceType == true + && (conversions.IdentityConversion(lhsType, rhsType) + || conversions.ExplicitConversion(lhsType, rhsType).IsReferenceConversion + || conversions.IdentityConversion(rhsType, lhsType) + || conversions.ExplicitConversion(rhsType, lhsType).IsReferenceConversion)) { // If it's a reference comparison if (op == BinaryOperatorType.Equality)