|
| 1 | +/** |
| 2 | + * @name Override with unmentioned parameter |
| 3 | + * @description A predicate that overrides the default behavior but doesn't mention a parameter is suspicious. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id ql/override-any |
| 7 | + * @precision high |
| 8 | + */ |
| 9 | + |
| 10 | +import ql |
| 11 | +import codeql_ql.performance.VarUnusedInDisjunctQuery |
| 12 | + |
| 13 | +AstNode param(Predicate pred, string name, Type t) { |
| 14 | + result = pred.getParameter(_) and |
| 15 | + result.(VarDecl).getName() = name and |
| 16 | + result.(VarDecl).getType() = t |
| 17 | + or |
| 18 | + result = pred.getReturnTypeExpr() and |
| 19 | + name = "result" and |
| 20 | + t = pred.getReturnType() |
| 21 | +} |
| 22 | + |
| 23 | +predicate hasAccess(Predicate pred, string name) { |
| 24 | + exists(param(pred, name, _).(VarDecl).getAnAccess()) |
| 25 | + or |
| 26 | + name = "result" and |
| 27 | + exists(param(pred, name, _)) and |
| 28 | + exists(ResultAccess res | res.getEnclosingPredicate() = pred) |
| 29 | +} |
| 30 | + |
| 31 | +from Predicate pred, AstNode param, string name, Type paramType |
| 32 | +where |
| 33 | + pred.hasAnnotation("override") and |
| 34 | + param = param(pred, name, paramType) and |
| 35 | + not hasAccess(pred, name) and |
| 36 | + not pred.getBody() instanceof NoneCall and |
| 37 | + exists(pred.getBody()) and |
| 38 | + not isSmallType(pred.getParent().(Class).getType()) and |
| 39 | + not isSmallType(paramType) |
| 40 | +select pred, "Override predicate doesn't mention $@. Maybe mention it in a 'exists(" + name + ")'?", |
| 41 | + param, name |
0 commit comments