diff --git a/ql/src/codeql_ruby/ast/Expr.qll b/ql/src/codeql_ruby/ast/Expr.qll index e48f091dfd1..9a0204aba6f 100644 --- a/ql/src/codeql_ruby/ast/Expr.qll +++ b/ql/src/codeql_ruby/ast/Expr.qll @@ -229,6 +229,8 @@ class Pair extends Expr, @pair { class Rescue extends Expr, @rescue { final override Rescue::Range range; + final override string getAPrimaryQlClass() { result = "Rescue" } + /** * Gets the `n`th exception to match, if any. For example `FirstError` or `SecondError` in: * ```rb @@ -271,3 +273,31 @@ class Rescue extends Expr, @rescue { */ final StmtSequence getBody() { result = range.getBody() } } + +/** + * An expression with a `rescue` modifier. For example: + * ```rb + * contents = read_file rescue "" + * ``` + */ +class RescueExpr extends Expr, @rescue_modifier { + final override RescueExpr::Range range; + + final override string getAPrimaryQlClass() { result = "RescueExpr" } + + /** + * Gets the body of this `RescueExpr`. + * ```rb + * body rescue handler + * ``` + */ + final Stmt getBody() { result = range.getBody() } + + /** + * Gets the exception handler of this `RescueExpr`. + * ```rb + * body rescue handler + * ``` + */ + final Stmt getHandler() { result = range.getHandler() } +} diff --git a/ql/src/codeql_ruby/ast/internal/AST.qll b/ql/src/codeql_ruby/ast/internal/AST.qll index 618aea0313a..2cc969581fb 100644 --- a/ql/src/codeql_ruby/ast/internal/AST.qll +++ b/ql/src/codeql_ruby/ast/internal/AST.qll @@ -69,8 +69,6 @@ module AstNode { or this instanceof Generated::Rational or - this instanceof Generated::RescueModifier - or this instanceof Generated::Subshell or this instanceof Generated::Undef diff --git a/ql/src/codeql_ruby/ast/internal/Expr.qll b/ql/src/codeql_ruby/ast/internal/Expr.qll index 9a7dfe22758..be03b7b7a99 100644 --- a/ql/src/codeql_ruby/ast/internal/Expr.qll +++ b/ql/src/codeql_ruby/ast/internal/Expr.qll @@ -290,6 +290,18 @@ module Rescue { } } +module RescueExpr { + class Range extends Expr::Range, @rescue_modifier { + final override Generated::RescueModifier generated; + + final Stmt getBody() { result = generated.getBody() } + + final Stmt getHandler() { result = generated.getHandler() } + + final override string toString() { result = "... rescue ..." } + } +} + module Pair { class Range extends Expr::Range, @pair { final override Generated::Pair generated; diff --git a/ql/test/library-tests/controlflow/graph/Cfg.expected b/ql/test/library-tests/controlflow/graph/Cfg.expected index 98762e036bd..5affc64fbcd 100644 --- a/ql/test/library-tests/controlflow/graph/Cfg.expected +++ b/ql/test/library-tests/controlflow/graph/Cfg.expected @@ -1534,7 +1534,7 @@ cfg.rb: #-----| -> Interpolation # 130| ... = ... -#-----| -> RescueModifier +#-----| -> ... rescue ... # 130| Constant #-----| -> ... = ... @@ -1542,7 +1542,7 @@ cfg.rb: # 130| 5 #-----| -> Constant -# 133| RescueModifier +# 133| ... rescue ... #-----| -> 1 # 133| ... / ...