Skip to content

Commit 563ffb8

Browse files
authored
Merge pull request #19010 from michaelnebel/csharp/useless-gethashcode-call
C#: Increase precision of `cs/useless-gethashcode-call`.
2 parents de2fb03 + dff66c7 commit 563ffb8

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

csharp/ql/src/Useless code/IntGetHashCode.ql

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@ import semmle.code.csharp.frameworks.System
1616
from MethodCall mc, IntegralType t
1717
where
1818
mc.getTarget() instanceof GetHashCodeMethod and
19-
t = mc.getQualifier().getType()
19+
t = mc.getQualifier().getType() and
20+
(
21+
t instanceof ByteType or
22+
t instanceof SByteType or
23+
t instanceof ShortType or
24+
t instanceof UShortType or
25+
t instanceof IntType
26+
)
2027
select mc, "Calling GetHashCode() on type " + t.toStringWithTypes() + " is redundant."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Increase query precision for `cs/useless-gethashcode-call` by not flagging calls to `GetHashCode` on `uint`, `long` and `ulong`.

csharp/ql/test/query-tests/Useless Code/IntGetHashCode/IntGetHashCode.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ class IntGetHashCode
33
void Test()
44
{
55
// These are all bad:
6+
default(int).GetHashCode(); // $ Alert
7+
default(short).GetHashCode(); // $ Alert
8+
default(ushort).GetHashCode(); // $ Alert
9+
default(byte).GetHashCode(); // $ Alert
10+
default(sbyte).GetHashCode(); // $ Alert
11+
12+
// These are all good:
613
default(uint).GetHashCode();
7-
default(int).GetHashCode();
814
default(long).GetHashCode();
915
default(ulong).GetHashCode();
10-
default(short).GetHashCode();
11-
default(ushort).GetHashCode();
12-
default(byte).GetHashCode();
13-
default(sbyte).GetHashCode();
14-
15-
// These are all good:
1616
default(double).GetHashCode();
1717
default(float).GetHashCode();
1818
default(char).GetHashCode();
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
| IntGetHashCode.cs:6:9:6:35 | call to method GetHashCode | Calling GetHashCode() on type uint is redundant. |
2-
| IntGetHashCode.cs:7:9:7:34 | call to method GetHashCode | Calling GetHashCode() on type int is redundant. |
3-
| IntGetHashCode.cs:8:9:8:35 | call to method GetHashCode | Calling GetHashCode() on type long is redundant. |
4-
| IntGetHashCode.cs:9:9:9:36 | call to method GetHashCode | Calling GetHashCode() on type ulong is redundant. |
5-
| IntGetHashCode.cs:10:9:10:36 | call to method GetHashCode | Calling GetHashCode() on type short is redundant. |
6-
| IntGetHashCode.cs:11:9:11:37 | call to method GetHashCode | Calling GetHashCode() on type ushort is redundant. |
7-
| IntGetHashCode.cs:12:9:12:35 | call to method GetHashCode | Calling GetHashCode() on type byte is redundant. |
8-
| IntGetHashCode.cs:13:9:13:36 | call to method GetHashCode | Calling GetHashCode() on type sbyte is redundant. |
1+
| IntGetHashCode.cs:6:9:6:34 | call to method GetHashCode | Calling GetHashCode() on type int is redundant. |
2+
| IntGetHashCode.cs:7:9:7:36 | call to method GetHashCode | Calling GetHashCode() on type short is redundant. |
3+
| IntGetHashCode.cs:8:9:8:37 | call to method GetHashCode | Calling GetHashCode() on type ushort is redundant. |
4+
| IntGetHashCode.cs:9:9:9:35 | call to method GetHashCode | Calling GetHashCode() on type byte is redundant. |
5+
| IntGetHashCode.cs:10:9:10:36 | call to method GetHashCode | Calling GetHashCode() on type sbyte is redundant. |
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Useless code/IntGetHashCode.ql
1+
query: Useless code/IntGetHashCode.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

0 commit comments

Comments
 (0)