Skip to content

Commit 27f0505

Browse files
committed
Ruby: test implicit return refactor
1 parent 0751408 commit 27f0505

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPrivate.qll

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,16 +1643,27 @@ private module ReturnNodes {
16431643
}
16441644
}
16451645

1646-
pragma[noinline]
1647-
private AstNode implicitReturn(Callable c, ExprNode n) {
1648-
exists(CfgNodes::ExprCfgNode en |
1649-
en = n.getExprNode() and
1650-
en.getASuccessor().(CfgNodes::AnnotatedExitNode).isNormal() and
1651-
n.(NodeImpl).getCfgScope() = c and
1652-
result = en.getExpr()
1653-
)
1654-
or
1655-
result = implicitReturn(c, n).getParent()
1646+
// pragma[noinline]
1647+
// private AstNode implicitReturn(Callable c, ExprNode n) {
1648+
// exists(CfgNodes::ExprCfgNode en |
1649+
// en = n.getExprNode() and
1650+
// en.getASuccessor().(CfgNodes::AnnotatedExitNode).isNormal() and
1651+
// n.(NodeImpl).getCfgScope() = c and
1652+
// result = en.getExpr()
1653+
// )
1654+
// or
1655+
// result = implicitReturn(c, n).getParent()
1656+
// }
1657+
// predicate implicitReturnNested(Callable c, ExprNode n, AstNode parent) {
1658+
// implicitReturn(c, n).getParent() = parent and
1659+
// c.getBody().getAStmt() = parent
1660+
// and not n.getExprNode().getExpr() = parent.getDesugared()
1661+
// }
1662+
private AstNode desugar(AstNode n) {
1663+
result = n.getDesugared()
1664+
or
1665+
not exists(n.getDesugared()) and
1666+
result = n
16561667
}
16571668

16581669
/**
@@ -1661,7 +1672,10 @@ private module ReturnNodes {
16611672
* last thing that is evaluated in the body of the callable.
16621673
*/
16631674
class ExprReturnNode extends SourceReturnNode, ExprNode {
1664-
ExprReturnNode() { exists(Callable c | implicitReturn(c, this) = c.getBody().getAStmt()) }
1675+
// ExprReturnNode() { exists(Callable c | implicitReturn(c, this) = c.getBody().getAStmt()) }
1676+
ExprReturnNode() {
1677+
this.getExprNode().getExpr() = desugar(any(Callable c).getBody().getLastStmt())
1678+
}
16651679

16661680
override ReturnKind getKindSource() {
16671681
exists(CfgScope scope | scope = this.(NodeImpl).getCfgScope() |

0 commit comments

Comments
 (0)