Skip to content

Commit

Permalink
Fix #3322: Add missing checks for equality comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Jan 13, 2025
1 parent b9f5090 commit 82db34d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="ProjectDecompiler\TargetFrameworkTests.cs" />
<Compile Include="TestAssemblyResolver.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" />
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
<Compile Include="TestCases\VBPretty\VBNonGenericForEach.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
18 changes: 18 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
6 changes: 5 additions & 1 deletion ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 82db34d

Please sign in to comment.