зеркало из https://github.com/github/codeql.git
Merge pull request #9827 from erik-krogh/overrideAny
QL: Query for detecting unused parameter in override methods
This commit is contained in:
Коммит
2291f18695
|
@ -0,0 +1,27 @@
|
|||
import ql
|
||||
|
||||
/**
|
||||
* Holds if we assume `t` is a small type, and
|
||||
* variables of this type are therefore not an issue in cartesian products.
|
||||
*/
|
||||
predicate isSmallType(Type t) {
|
||||
t.getName() = "string" // DataFlow::Configuration and the like
|
||||
or
|
||||
exists(NewType newType | newType = t.getDeclaration() |
|
||||
forex(NewTypeBranch branch | branch = newType.getABranch() | branch.getArity() = 0)
|
||||
)
|
||||
or
|
||||
t.getName() = "boolean"
|
||||
or
|
||||
exists(NewType newType | newType = t.getDeclaration() |
|
||||
forex(NewTypeBranch branch | branch = newType.getABranch() |
|
||||
isSmallType(branch.getReturnType())
|
||||
)
|
||||
)
|
||||
or
|
||||
exists(NewTypeBranch branch | t = branch.getReturnType() |
|
||||
forall(Type param | param = branch.getParameterType(_) | isSmallType(param))
|
||||
)
|
||||
or
|
||||
isSmallType(t.getASuperType())
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
*/
|
||||
|
||||
import ql
|
||||
import codeql_ql.performance.VarUnusedInDisjunctQuery
|
||||
|
||||
/**
|
||||
* Holds if `node` bind `var` in a (transitive) child node.
|
||||
|
@ -48,32 +49,6 @@ predicate alwaysBindsVar(VarDef var, AstNode node) {
|
|||
exists(IfFormula ifForm | ifForm = node | alwaysBindsVar(var, ifForm.getCondition()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if we assume `t` is a small type, and
|
||||
* variables of this type are therefore not an issue in cartesian products.
|
||||
*/
|
||||
predicate isSmallType(Type t) {
|
||||
t.getName() = "string" // DataFlow::Configuration and the like
|
||||
or
|
||||
exists(NewType newType | newType = t.getDeclaration() |
|
||||
forex(NewTypeBranch branch | branch = newType.getABranch() | branch.getArity() = 0)
|
||||
)
|
||||
or
|
||||
t.getName() = "boolean"
|
||||
or
|
||||
exists(NewType newType | newType = t.getDeclaration() |
|
||||
forex(NewTypeBranch branch | branch = newType.getABranch() |
|
||||
isSmallType(branch.getReturnType())
|
||||
)
|
||||
)
|
||||
or
|
||||
exists(NewTypeBranch branch | t = branch.getReturnType() |
|
||||
forall(Type param | param = branch.getParameterType(_) | isSmallType(param))
|
||||
)
|
||||
or
|
||||
isSmallType(t.getASuperType())
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if `pred` is inlined.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* @name Override with unmentioned parameter
|
||||
* @description A predicate that overrides the default behavior but doesn't mention a parameter is suspicious.
|
||||
* @kind problem
|
||||
* @problem.severity warning
|
||||
* @id ql/override-any
|
||||
* @precision high
|
||||
*/
|
||||
|
||||
import ql
|
||||
import codeql_ql.performance.VarUnusedInDisjunctQuery
|
||||
|
||||
AstNode param(Predicate pred, string name, Type t) {
|
||||
result = pred.getParameter(_) and
|
||||
result.(VarDecl).getName() = name and
|
||||
result.(VarDecl).getType() = t
|
||||
or
|
||||
result = pred.getReturnTypeExpr() and
|
||||
name = "result" and
|
||||
t = pred.getReturnType()
|
||||
}
|
||||
|
||||
predicate hasAccess(Predicate pred, string name) {
|
||||
exists(param(pred, name, _).(VarDecl).getAnAccess())
|
||||
or
|
||||
name = "result" and
|
||||
exists(param(pred, name, _)) and
|
||||
exists(ResultAccess res | res.getEnclosingPredicate() = pred)
|
||||
}
|
||||
|
||||
from Predicate pred, AstNode param, string name, Type paramType
|
||||
where
|
||||
pred.hasAnnotation("override") and
|
||||
param = param(pred, name, paramType) and
|
||||
not hasAccess(pred, name) and
|
||||
not pred.getBody() instanceof NoneCall and
|
||||
exists(pred.getBody()) and
|
||||
not isSmallType(pred.getParent().(Class).getType()) and
|
||||
not isSmallType(paramType)
|
||||
select pred, "Override predicate doesn't mention $@. Maybe mention it in a 'exists(" + name + ")'?",
|
||||
param, name
|
Загрузка…
Ссылка в новой задаче