forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInconsistentEqualsGetHashCode.ql
More file actions
30 lines (28 loc) · 942 Bytes
/
InconsistentEqualsGetHashCode.ql
File metadata and controls
30 lines (28 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @name Inconsistent Equals(object) and GetHashCode()
* @description If a class overrides only one of 'Equals(object)' and 'GetHashCode()', it may mean that
* 'Equals(object)' and 'GetHashCode()' are inconsistent.
* @kind problem
* @problem.severity warning
* @precision medium
* @id cs/inconsistent-equals-and-gethashcode
* @tags reliability
* maintainability
* external/cwe/cwe-581
*/
import csharp
import semmle.code.csharp.frameworks.System
from Class c, Method present, string missing
where
c.isSourceDeclaration() and
(
present = c.getAMethod().(EqualsMethod) and
not c.getAMethod() instanceof GetHashCodeMethod and
missing = "GetHashCode()"
or
present = c.getAMethod().(GetHashCodeMethod) and
not implementsEquals(c) and
missing = "Equals(object)"
)
select c, "Class '" + c.getName() + "' overrides $@, but not " + missing + ".", present,
present.getName()