Skip to content

Commit 8b0bc67

Browse files
committed
Remove PatternGuard class
1 parent dca1e34 commit 8b0bc67

File tree

3 files changed

+41
-94
lines changed

3 files changed

+41
-94
lines changed

ruby/ql/lib/codeql/ruby/ast/Control.qll

Lines changed: 24 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -539,94 +539,50 @@ class InClause extends Expr, TInClause {
539539
final CasePattern getPattern() { toGenerated(result) = g.getPattern() }
540540

541541
/**
542-
* Gets the pattern guard in this case-in expression. In the
543-
* following example, the pattern guard is `x > 10`.
542+
* Gets the pattern guard condition in this case-in expression. In the
543+
* following example, the pattern guard condition is `x > 10`.
544544
* ```rb
545545
* case foo
546546
* in [ x ] if x > 10 then ...
547+
* in [ x ] unless x < 10 then ...
547548
* end
548549
* ```
549550
*/
550-
final PatternGuard getPatternGuard() { toGenerated(result) = g.getGuard() }
551+
final Expr getCondition() { toGenerated(result) = g.getGuard().getAFieldOrChild() }
551552

552-
final override string toString() { result = "in ... then ..." }
553-
554-
override AstNode getAChild(string pred) {
555-
result = super.getAChild(pred)
556-
or
557-
pred = "getBody" and result = this.getBody()
558-
or
559-
pred = "getPattern" and result = this.getPattern()
560-
or
561-
pred = "getGuard" and result = this.getPatternGuard()
562-
}
563-
}
553+
/**
554+
* Holds if the pattern guard in this case-in expression is an `if` condition. For example:
555+
* ```rb
556+
* case foo
557+
* in [ x ] if x > 10 then ...
558+
* end
559+
* ```
560+
*/
561+
predicate hasIfCondition() { g.getGuard() instanceof Ruby::IfGuard }
564562

565-
/**
566-
* A guard used in pattern matching. For example:
567-
* ```rb
568-
* in pattern if guard
569-
* in pattern unless guard
570-
* ```
571-
*/
572-
class PatternGuard extends AstNode, TPatternGuard {
573563
/**
574-
* Gets the condition expression. For example, the result is `foo` in the
575-
* following:
564+
* Holds if the pattern guard in this case-in expression is an `unless` condition. For example:
576565
* ```rb
577-
* if foo
578-
* unless foo
566+
* case foo
567+
* in [ x ] unless x < 10 then ...
568+
* end
579569
* ```
580570
*/
581-
Expr getCondition() { none() }
571+
predicate hasUnlessCondition() { g.getGuard() instanceof Ruby::UnlessGuard }
572+
573+
final override string toString() { result = "in ... then ..." }
582574

583575
override AstNode getAChild(string pred) {
584576
result = super.getAChild(pred)
585577
or
578+
pred = "getBody" and result = this.getBody()
579+
or
580+
pred = "getPattern" and result = this.getPattern()
581+
or
586582
pred = "getCondition" and result = this.getCondition()
587583
}
588584
}
589585

590-
/**
591-
* An `if` pattern guard. For example:
592-
* ```rb
593-
* case foo
594-
* in [ bar] if bar > 10
595-
* end
596-
* ```
597-
*/
598-
class IfGuard extends PatternGuard, TIfGuard {
599-
private Ruby::IfGuard g;
600-
601-
IfGuard() { this = TIfGuard(g) }
602-
603-
final override Expr getCondition() { toGenerated(result) = g.getCondition() }
604-
605-
final override string getAPrimaryQlClass() { result = "IfGuard" }
606-
607-
final override string toString() { result = "if ..." }
608-
}
609-
610-
/**
611-
* An `unless` pattern guard. For example:
612-
* ```rb
613-
* case foo
614-
* in [ bar] unless bar > 10
615-
* end
616-
* ```
617-
*/
618-
class UnlessGuard extends PatternGuard, TUnlessGuard {
619-
private Ruby::UnlessGuard g;
620-
621-
UnlessGuard() { this = TUnlessGuard(g) }
622-
623-
final override Expr getCondition() { toGenerated(result) = g.getCondition() }
624-
625-
final override string getAPrimaryQlClass() { result = "UnlessGuard" }
626-
627-
final override string toString() { result = "unless ..." }
628-
}
629-
630586
/**
631587
* A loop. That is, a `for` loop, a `while` or `until` loop, or their
632588
* expression-modifier variants.

ruby/ql/lib/codeql/ruby/ast/internal/AST.qll

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ private module Cached {
161161
THereDoc(Ruby::HeredocBeginning g) or
162162
TIdentifierMethodCall(Ruby::Identifier g) { isIdentifierMethodCall(g) } or
163163
TIf(Ruby::If g) or
164-
TIfGuard(Ruby::IfGuard g) or
165164
TIfModifierExpr(Ruby::IfModifier g) or
166165
TInClause(Ruby::InClause g) or
167166
TInstanceVariableAccessReal(Ruby::InstanceVariable g, AST::InstanceVariable v) {
@@ -305,7 +304,6 @@ private module Cached {
305304
TUnaryPlusExpr(Ruby::Unary g) { g instanceof @ruby_unary_plus } or
306305
TUndefStmt(Ruby::Undef g) or
307306
TUnlessExpr(Ruby::Unless g) or
308-
TUnlessGuard(Ruby::UnlessGuard g) or
309307
TUnlessModifierExpr(Ruby::UnlessModifier g) or
310308
TUntilExpr(Ruby::Until g) or
311309
TUntilModifierExpr(Ruby::UntilModifier g) or
@@ -332,25 +330,24 @@ private module Cached {
332330
TForwardArgument or TGEExpr or TGTExpr or TGlobalVariableAccessReal or
333331
THashKeySymbolLiteral or THashLiteral or THashPattern or THashSplatExpr or
334332
THashSplatNilParameter or THashSplatParameter or THereDoc or TIdentifierMethodCall or TIf or
335-
TIfGuard or TIfModifierExpr or TInClause or TInstanceVariableAccessReal or
336-
TIntegerLiteralReal or TKeywordParameter or TLEExpr or TLShiftExprReal or TLTExpr or
337-
TLambda or TLeftAssignmentList or TLine or TLocalVariableAccessReal or
338-
TLogicalAndExprReal or TLogicalOrExprReal or TMethod or TModuleDeclaration or
339-
TModuloExprReal or TMulExprReal or TNEExpr or TNextStmt or TNilLiteral or
340-
TNoRegExpMatchExpr or TNotExpr or TOptionalParameter or TPair or TParenthesizedExpr or
341-
TRShiftExprReal or TRangeLiteralReal or TRationalLiteral or TRedoStmt or TRegExpLiteral or
342-
TRegExpMatchExpr or TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or
343-
TRegularSuperCall or TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or
333+
TIfModifierExpr or TInClause or TInstanceVariableAccessReal or TIntegerLiteralReal or
334+
TKeywordParameter or TLEExpr or TLShiftExprReal or TLTExpr or TLambda or
335+
TLeftAssignmentList or TLine or TLocalVariableAccessReal or TLogicalAndExprReal or
336+
TLogicalOrExprReal or TMethod or TModuleDeclaration or TModuloExprReal or TMulExprReal or
337+
TNEExpr or TNextStmt or TNilLiteral or TNoRegExpMatchExpr or TNotExpr or
338+
TOptionalParameter or TPair or TParenthesizedExpr or TRShiftExprReal or TRangeLiteralReal or
339+
TRationalLiteral or TRedoStmt or TRegExpLiteral or TRegExpMatchExpr or
340+
TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or TRegularSuperCall or
341+
TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or
344342
TScopeResolutionConstantAccess or TScopeResolutionMethodCall or TSelfReal or
345343
TSimpleParameterReal or TSimpleSymbolLiteral or TSingletonClass or TSingletonMethod or
346344
TSpaceshipExpr or TSplatExprReal or TSplatParameter or TStringArrayLiteral or
347345
TStringConcatenation or TStringEscapeSequenceComponent or TStringInterpolationComponent or
348346
TStringTextComponent or TSubExprReal or TSubshellLiteral or TSymbolArrayLiteral or
349347
TTernaryIfExpr or TThen or TTokenConstantAccess or TTokenMethodName or TTokenSuperCall or
350348
TToplevel or TTrueLiteral or TTuplePatternParameter or TUnaryMinusExpr or TUnaryPlusExpr or
351-
TUndefStmt or TUnlessExpr or TUnlessGuard or TUnlessModifierExpr or TUntilExpr or
352-
TUntilModifierExpr or TVariableReferencePattern or TWhenExpr or TWhileExpr or
353-
TWhileModifierExpr or TYieldCall;
349+
TUndefStmt or TUnlessExpr or TUnlessModifierExpr or TUntilExpr or TUntilModifierExpr or
350+
TVariableReferencePattern or TWhenExpr or TWhileExpr or TWhileModifierExpr or TYieldCall;
354351

355352
class TAstNodeSynth =
356353
TAddExprSynth or TAssignExprSynth or TBitwiseAndExprSynth or TBitwiseOrExprSynth or
@@ -441,7 +438,6 @@ private module Cached {
441438
n = THashSplatParameter(result) or
442439
n = THereDoc(result) or
443440
n = TIdentifierMethodCall(result) or
444-
n = TIfGuard(result) or
445441
n = TIfModifierExpr(result) or
446442
n = TIf(result) or
447443
n = TInClause(result) or
@@ -513,7 +509,6 @@ private module Cached {
513509
n = TUnaryPlusExpr(result) or
514510
n = TUndefStmt(result) or
515511
n = TUnlessExpr(result) or
516-
n = TUnlessGuard(result) or
517512
n = TUnlessModifierExpr(result) or
518513
n = TUntilExpr(result) or
519514
n = TUntilModifierExpr(result) or
@@ -642,8 +637,6 @@ class TControlExpr = TConditionalExpr or TCaseExpr or TCaseMatch or TLoop;
642637
class TConditionalExpr =
643638
TIfExpr or TUnlessExpr or TIfModifierExpr or TUnlessModifierExpr or TTernaryIfExpr;
644639

645-
class TPatternGuard = TIfGuard or TUnlessGuard;
646-
647640
class TIfExpr = TIf or TElsif;
648641

649642
class TConditionalLoop = TWhileExpr or TUntilExpr or TWhileModifierExpr or TUntilModifierExpr;

ruby/ql/test/library-tests/ast/Ast.expected

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,18 +733,16 @@ control/cases.rb:
733733
# 31| getReceiver: [Self, SelfVariableAccess] self
734734
# 32| getBranch/getInClause: [InClause] in ... then ...
735735
# 32| getPattern: [LocalVariableAccess] x
736-
# 32| getGuard: [UnlessGuard] unless ...
737-
# 32| getCondition: [LTExpr] ... < ...
738-
# 32| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
739-
# 32| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
736+
# 32| getCondition: [LTExpr] ... < ...
737+
# 32| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
738+
# 32| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
740739
# 32| getBody: [StmtSequence] then ...
741740
# 33| getStmt: [BooleanLiteral] true
742741
# 34| getBranch/getInClause: [InClause] in ... then ...
743742
# 34| getPattern: [LocalVariableAccess] x
744-
# 34| getGuard: [IfGuard] if ...
745-
# 34| getCondition: [LTExpr] ... < ...
746-
# 34| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
747-
# 34| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
743+
# 34| getCondition: [LTExpr] ... < ...
744+
# 34| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
745+
# 34| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
748746
# 34| getBody: [StmtSequence] then ...
749747
# 35| getStmt: [BooleanLiteral] true
750748
# 36| getBranch/getElseBranch: [StmtSequence] else ...

0 commit comments

Comments
 (0)