Python: ObjectAPI to ValueAPI: ReturnValueIgnore: Moves getAnInferredType to CallableObjectInternal

This commit is contained in:
Rebecca Valentine
2020-04-01 08:45:27 -07:00
parent 12377badf9
commit 838e37ca98
3 changed files with 9 additions and 40 deletions

View File

@@ -15,6 +15,7 @@
*/
import python
import semmle.python.objects.Callables
predicate meaningful_return_value(Expr val) {
val instanceof Name
@@ -44,7 +45,7 @@ predicate returns_meaningful_value(FunctionValue f) {
or
/* Is f a builtin function that returns something other than None?
* Ignore __import__ as it is often called purely for side effects */
f.isBuiltin() and f.getAnInferredReturnType() != ClassValue::nonetype() and not f.getName() = "__import__"
f.isBuiltin() and f.(CallableObjectInternal).getAnInferredReturnType() != ClassValue::nonetype() and not f.getName() = "__import__"
)
}

View File

@@ -49,6 +49,13 @@ abstract class CallableObjectInternal extends ObjectInternal {
/* Callables aren't iterable */
override ObjectInternal getIterNext() { none() }
/** Gets a class that this function may return */
ClassValue getAnInferredReturnType() {
result = TBuiltinClassObject(this.(BuiltinFunctionObjectInternal).getReturnType())
or
result = TBuiltinClassObject(this.(BuiltinMethodObjectInternal).getReturnType())
}
}
/** Class representing Python functions */

View File

@@ -581,13 +581,6 @@ abstract class FunctionValue extends CallableValue {
exists(Expr expr, AstNode origin | expr.pointsTo(this, origin) | not origin instanceof Lambda)
)
}
/** Gets a class that this function may return */
ClassValue getAnInferredReturnType() {
result = this.(BuiltinFunctionValue).getAReturnType()
or
result = this.(BuiltinMethodValue).getAReturnType()
}
}
/** Class representing Python functions */
@@ -627,29 +620,6 @@ class BuiltinFunctionValue extends FunctionValue {
override int minParameters() { none() }
override int maxParameters() { none() }
ClassValue getAReturnType() {
/* Enumerate the types of a few builtin functions, that the CPython analysis misses.
*/
this = TBuiltinFunctionObject(Builtin::builtin("hex")) and result = ClassValue::str()
or
this = TBuiltinFunctionObject(Builtin::builtin("oct")) and result = ClassValue::str()
or
this = TBuiltinFunctionObject(Builtin::builtin("intern")) and result = ClassValue::str()
or
/* Fix a few minor inaccuracies in the CPython analysis */
exists(Builtin mthd, Builtin cls | this = TBuiltinFunctionObject(mthd) and result = TBuiltinClassObject(cls)
| ext_rettype(mthd, cls)) and
not (
this = TBuiltinFunctionObject(Builtin::builtin("__import__")) and result = ClassValue::nonetype()
or
this = TBuiltinFunctionObject(Builtin::builtin("compile")) and result = ClassValue::nonetype()
or
this = TBuiltinFunctionObject(Builtin::builtin("sum"))
or
this = TBuiltinFunctionObject(Builtin::builtin("filter"))
)
}
}
/** Class representing builtin methods, such as `list.append` or `set.add` */
@@ -667,15 +637,6 @@ class BuiltinMethodValue extends FunctionValue {
override int minParameters() { none() }
override int maxParameters() { none() }
ClassValue getAReturnType() {
exists(Builtin mthd, Builtin cls |
this = TBuiltinMethodObject(mthd) and
result = TBuiltinClassObject(cls)
|
ext_rettype(mthd, cls)
)
}
}
/**