Skip to content

C#: Increase precision of cs/useless-gethashcode-call. #19010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion csharp/ql/src/Useless code/IntGetHashCode.ql
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ class IntGetHashCode
void Test()
{
// These are all bad:
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(int).GetHashCode();
default(long).GetHashCode();
default(ulong).GetHashCode();
default(short).GetHashCode();
default(ushort).GetHashCode();
default(byte).GetHashCode();
default(sbyte).GetHashCode();

// These are all good:
default(double).GetHashCode();
default(float).GetHashCode();
default(char).GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -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. |
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Useless code/IntGetHashCode.ql
query: Useless code/IntGetHashCode.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql