Skip to content

Commit fdbc72a

Browse files
Copilothvitved
andcommitted
Remove ObjectType parameter checks from System.qll methods
Removes instanceof ObjectType checks from method signatures in System.qll to support nullable reference types (object?). This fixes false positives when Equals(object?) is used instead of Equals(object), as they are the same underlying type with different nullability annotations. Fixes: - SystemIComparableInterface.getCompareToMethod() - SystemObjectClass.getEqualsMethod() - SystemObjectClass.getStaticEqualsMethod() - SystemObjectClass.getReferenceEqualsMethod() Co-authored-by: hvitved <3667920+hvitved@users.noreply.github.com>
1 parent c20b948 commit fdbc72a

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

csharp/ql/lib/semmle/code/csharp/frameworks/System.qll

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ class SystemIComparableInterface extends SystemInterface {
144144
result.getDeclaringType() = this and
145145
result.hasName("CompareTo") and
146146
result.getNumberOfParameters() = 1 and
147-
result.getParameter(0).getType() instanceof ObjectType and
148147
result.getReturnType() instanceof IntType
149148
}
150149
}
@@ -263,7 +262,6 @@ class SystemObjectClass extends SystemClass instanceof ObjectType {
263262
result.getDeclaringType() = this and
264263
result.hasName("Equals") and
265264
result.getNumberOfParameters() = 1 and
266-
result.getParameter(0).getType() instanceof ObjectType and
267265
result.getReturnType() instanceof BoolType
268266
}
269267

@@ -273,8 +271,6 @@ class SystemObjectClass extends SystemClass instanceof ObjectType {
273271
result.getDeclaringType() = this and
274272
result.hasName("Equals") and
275273
result.getNumberOfParameters() = 2 and
276-
result.getParameter(0).getType() instanceof ObjectType and
277-
result.getParameter(1).getType() instanceof ObjectType and
278274
result.getReturnType() instanceof BoolType
279275
}
280276

@@ -284,8 +280,6 @@ class SystemObjectClass extends SystemClass instanceof ObjectType {
284280
result.getDeclaringType() = this and
285281
result.hasName("ReferenceEquals") and
286282
result.getNumberOfParameters() = 2 and
287-
result.getParameter(0).getType() instanceof ObjectType and
288-
result.getParameter(1).getType() instanceof ObjectType and
289283
result.getReturnType() instanceof BoolType
290284
}
291285

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
3+
#nullable enable
4+
5+
namespace Test
6+
{
7+
class TestClass1 : IEquatable<TestClass1>
8+
{
9+
private int field1;
10+
11+
public bool Equals(TestClass1? param1)
12+
{
13+
return param1 != null && field1 == param1.field1;
14+
}
15+
16+
public override bool Equals(object? param2)
17+
{
18+
return param2 is TestClass1 tc && Equals(tc);
19+
}
20+
21+
public override int GetHashCode()
22+
{
23+
return field1;
24+
}
25+
}
26+
27+
class TestClass2
28+
{
29+
private string field2;
30+
31+
public TestClass2(string s)
32+
{
33+
field2 = s;
34+
}
35+
36+
public override bool Equals(object? param3)
37+
{
38+
return param3 is TestClass2 tc && field2 == tc.field2;
39+
}
40+
41+
public override int GetHashCode()
42+
{
43+
return field2?.GetHashCode() ?? 0;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)