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
31 lines (30 loc) · 969 Bytes
/
InconsistentEqualsGetHashCode.ql
File metadata and controls
31 lines (30 loc) · 969 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
31
/**
* @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 = (EqualsMethod)c.getAMethod() and
not c.getAMethod() instanceof GetHashCodeMethod and
missing = "GetHashCode()"
) or
(
present = (GetHashCodeMethod)c.getAMethod() and
not implementsEquals(c) and
missing = "Equals(object)"
)
)
select c, "Class '" + c.getName() + "' overrides $@, but not " + missing + ".",
present, present.getName()