-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathInconsistentCompareTo.ql
29 lines (27 loc) · 1.05 KB
/
InconsistentCompareTo.ql
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
/**
* @name Inconsistent CompareTo and Equals
* @description If a class implements 'IComparable.CompareTo' but does not override 'Equals', the two can be inconsistent.
* @kind problem
* @problem.severity warning
* @precision medium
* @id cs/inconsistent-compareto-and-equals
* @tags reliability
* maintainability
*/
import semmle.code.csharp.frameworks.System
from Class c, Method compareTo, Method compareToImpl
where
c.fromSource() and
(
compareTo = any(SystemIComparableInterface i).getCompareToMethod()
or
compareTo = any(SystemIComparableTInterface i).getAConstructedGeneric().getAMethod() and
compareTo.getUnboundDeclaration() = any(SystemIComparableTInterface i).getCompareToMethod()
) and
compareToImpl = c.getAMethod() and
compareToImpl = compareTo.getAnUltimateImplementor() and
not compareToImpl.isAbstract() and
not c.getAMethod() = any(SystemObjectClass o).getEqualsMethod().getAnOverrider+()
select c,
"Class " + c.getName() +
" implements CompareTo but does not override Equals; the two could be inconsistent."