Merge pull request #19010 from michaelnebel/csharp/useless-gethashcode-call

C#: Increase precision of `cs/useless-gethashcode-call`.
This commit is contained in:
Michael Nebel
2025-03-14 08:44:38 +01:00
committed by GitHub
5 changed files with 27 additions and 18 deletions

View File

@@ -16,5 +16,12 @@ import semmle.code.csharp.frameworks.System
from MethodCall mc, IntegralType t
where
mc.getTarget() instanceof GetHashCodeMethod and
t = mc.getQualifier().getType()
t = mc.getQualifier().getType() and
(
t instanceof ByteType or
t instanceof SByteType or
t instanceof ShortType or
t instanceof UShortType or
t instanceof IntType
)
select mc, "Calling GetHashCode() on type " + t.toStringWithTypes() + " is redundant."

View File

@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Increase query precision for `cs/useless-gethashcode-call` by not flagging calls to `GetHashCode` on `uint`, `long` and `ulong`.

View File

@@ -3,16 +3,16 @@ class IntGetHashCode
void Test()
{
// These are all bad:
default(uint).GetHashCode();
default(int).GetHashCode();
default(long).GetHashCode();
default(ulong).GetHashCode();
default(short).GetHashCode();
default(ushort).GetHashCode();
default(byte).GetHashCode();
default(sbyte).GetHashCode();
default(int).GetHashCode(); // $ Alert
default(short).GetHashCode(); // $ Alert
default(ushort).GetHashCode(); // $ Alert
default(byte).GetHashCode(); // $ Alert
default(sbyte).GetHashCode(); // $ Alert
// These are all good:
default(uint).GetHashCode();
default(long).GetHashCode();
default(ulong).GetHashCode();
default(double).GetHashCode();
default(float).GetHashCode();
default(char).GetHashCode();

View File

@@ -1,8 +1,5 @@
| IntGetHashCode.cs:6:9:6:35 | call to method GetHashCode | Calling GetHashCode() on type uint is redundant. |
| IntGetHashCode.cs:7:9:7:34 | call to method GetHashCode | Calling GetHashCode() on type int is redundant. |
| IntGetHashCode.cs:8:9:8:35 | call to method GetHashCode | Calling GetHashCode() on type long is redundant. |
| IntGetHashCode.cs:9:9:9:36 | call to method GetHashCode | Calling GetHashCode() on type ulong is redundant. |
| IntGetHashCode.cs:10:9:10:36 | call to method GetHashCode | Calling GetHashCode() on type short is redundant. |
| IntGetHashCode.cs:11:9:11:37 | call to method GetHashCode | Calling GetHashCode() on type ushort is redundant. |
| IntGetHashCode.cs:12:9:12:35 | call to method GetHashCode | Calling GetHashCode() on type byte is redundant. |
| IntGetHashCode.cs:13:9:13:36 | call to method GetHashCode | Calling GetHashCode() on type sbyte is redundant. |
| IntGetHashCode.cs:6:9:6:34 | call to method GetHashCode | Calling GetHashCode() on type int is redundant. |
| IntGetHashCode.cs:7:9:7:36 | call to method GetHashCode | Calling GetHashCode() on type short is redundant. |
| IntGetHashCode.cs:8:9:8:37 | call to method GetHashCode | Calling GetHashCode() on type ushort is redundant. |
| IntGetHashCode.cs:9:9:9:35 | call to method GetHashCode | Calling GetHashCode() on type byte is redundant. |
| IntGetHashCode.cs:10:9:10:36 | call to method GetHashCode | Calling GetHashCode() on type sbyte is redundant. |

View File

@@ -1 +1,2 @@
Useless code/IntGetHashCode.ql
query: Useless code/IntGetHashCode.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql