@@ -22,7 +22,9 @@ predicate meaningful_return_value(Expr val) {
2222 or
2323 val instanceof BooleanLiteral
2424 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+ )
2628 or
2729 not exists ( FunctionValue callee | val = callee .getACall ( ) .getNode ( ) ) and not val instanceof Name
2830}
@@ -34,18 +36,22 @@ predicate used_value(Expr val) {
3436 )
3537}
3638
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
4041 (
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__"
4955 )
5056}
5157
@@ -59,17 +65,19 @@ predicate wrapped_in_try_except(ExprStmt call) {
5965}
6066
6167from 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