-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathIncompleteOrdering.ql
More file actions
57 lines (50 loc) · 1.62 KB
/
IncompleteOrdering.ql
File metadata and controls
57 lines (50 loc) · 1.62 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @name Incomplete ordering
* @description Class defines one or more ordering method but does not define all 4 ordering comparison methods
* @kind problem
* @tags quality
* reliability
* correctness
* @problem.severity warning
* @sub-severity low
* @precision very-high
* @id py/incomplete-ordering
*/
import python
import semmle.python.dataflow.new.internal.DataFlowDispatch
import semmle.python.ApiGraphs
predicate totalOrdering(Class cls) {
cls.getADecorator() =
API::moduleImport("functools").getMember("total_ordering").asSource().asExpr()
}
Function getMethod(Class cls, string name) {
result = cls.getAMethod() and
result.getName() = name
}
predicate definesStrictOrdering(Class cls, Function meth) {
meth = getMethod(cls, "__lt__")
or
not exists(getMethod(cls, "__lt__")) and
meth = getMethod(cls, "__gt__")
}
predicate definesNonStrictOrdering(Class cls, Function meth) {
meth = getMethod(cls, "__le__")
or
not exists(getMethod(cls, "__le__")) and
meth = getMethod(cls, "__ge__")
}
predicate missingComparison(Class cls, Function defined, string missing) {
definesStrictOrdering(cls, defined) and
not definesNonStrictOrdering(getADirectSuperclass*(cls), _) and
missing = "__le__ or __ge__"
or
definesNonStrictOrdering(cls, defined) and
not definesStrictOrdering(getADirectSuperclass*(cls), _) and
missing = "__lt__ or __gt__"
}
from Class cls, Function defined, string missing
where
not totalOrdering(cls) and
missingComparison(cls, defined, missing)
select cls, "This class implements $@, but does not implement an " + missing + " method.", defined,
defined.getName()