@@ -22,7 +22,9 @@ predicate meaningful_return_value(Expr val) {
22
22
or
23
23
val instanceof BooleanLiteral
24
24
or
25
- exists ( FunctionValue callee | val = callee .getACall ( ) .getNode ( ) and returns_meaningful_value ( callee ) )
25
+ exists ( FunctionValue callee |
26
+ val = callee .getACall ( ) .getNode ( ) and returns_meaningful_value ( callee )
27
+ )
26
28
or
27
29
not exists ( FunctionValue callee | val = callee .getACall ( ) .getNode ( ) ) and not val instanceof Name
28
30
}
@@ -34,18 +36,22 @@ predicate used_value(Expr val) {
34
36
)
35
37
}
36
38
37
- predicate returns_meaningful_value ( FunctionValue f ) {
38
- not exists ( f .getScope ( ) .getFallthroughNode ( ) )
39
- and
39
+ predicate returns_meaningful_value ( FunctionValue f ) {
40
+ not exists ( f .getScope ( ) .getFallthroughNode ( ) ) and
40
41
(
41
- exists ( Return ret , Expr val | ret .getScope ( ) = f .getScope ( ) and val = ret .getValue ( ) |
42
- meaningful_return_value ( val ) and
43
- not used_value ( val )
44
- )
45
- or
46
- /* Is f a builtin function that returns something other than None?
47
- * Ignore __import__ as it is often called purely for side effects */
48
- f .isBuiltin ( ) and f .( CallableObjectInternal ) .getAnInferredReturnType ( ) != ClassValue:: nonetype ( ) and not f .getName ( ) = "__import__"
42
+ exists ( Return ret , Expr val | ret .getScope ( ) = f .getScope ( ) and val = ret .getValue ( ) |
43
+ meaningful_return_value ( val ) and
44
+ not used_value ( val )
45
+ )
46
+ or
47
+ /*
48
+ * Is f a builtin function that returns something other than None?
49
+ * Ignore __import__ as it is often called purely for side effects
50
+ */
51
+
52
+ f .isBuiltin ( ) and
53
+ f .( CallableObjectInternal ) .getAnInferredReturnType ( ) != ClassValue:: nonetype ( ) and
54
+ not f .getName ( ) = "__import__"
49
55
)
50
56
}
51
57
@@ -59,17 +65,19 @@ predicate wrapped_in_try_except(ExprStmt call) {
59
65
}
60
66
61
67
from ExprStmt call , FunctionValue callee , float percentage_used , int total
62
- where call .getValue ( ) = callee .getACall ( ) .getNode ( ) and returns_meaningful_value ( callee ) and
63
- not wrapped_in_try_except ( call ) and
64
- exists ( int unused |
65
- unused = count ( ExprStmt e | e .getValue ( ) .getAFlowNode ( ) = callee .getACall ( ) ) and
66
- total = count ( callee .getACall ( ) ) |
67
- percentage_used = ( 100.0 * ( total - unused ) / total ) .floor ( )
68
- ) and
69
- /* Report an alert if we see at least 5 calls and the return value is used in at least 3/4 of those calls. */
70
- percentage_used >= 75 and
71
- total >= 5
72
-
73
- select call , "Call discards return value of function $@. The result is used in " + percentage_used .toString ( ) + "% of calls." ,
74
- callee , callee .getName ( )
75
-
68
+ where
69
+ call .getValue ( ) = callee .getACall ( ) .getNode ( ) and
70
+ returns_meaningful_value ( callee ) and
71
+ not wrapped_in_try_except ( call ) and
72
+ exists ( int unused |
73
+ unused = count ( ExprStmt e | e .getValue ( ) .getAFlowNode ( ) = callee .getACall ( ) ) and
74
+ total = count ( callee .getACall ( ) )
75
+ |
76
+ percentage_used = ( 100.0 * ( total - unused ) / total ) .floor ( )
77
+ ) and
78
+ /* Report an alert if we see at least 5 calls and the return value is used in at least 3/4 of those calls. */
79
+ percentage_used >= 75 and
80
+ total >= 5
81
+ select call ,
82
+ "Call discards return value of function $@. The result is used in " + percentage_used .toString ( ) +
83
+ "% of calls." , callee , callee .getName ( )
0 commit comments