@@ -8,6 +8,7 @@ import java
88private import codeql.controlflow.ControlFlowGraph
99private import codeql.controlflow.SuccessorType
1010private import controlflow.internal.Preconditions
11+ private import controlflow.internal.SwitchCases
1112
1213private module Cfg0 = Make0< Location , Ast > ;
1314
@@ -41,11 +42,17 @@ private module Ast implements AstSig<Location> {
4142 AstNode getChild ( AstNode n , int index ) {
4243 not skipControlFlow ( result ) and
4344 not skipControlFlow ( n ) and
45+ not isBindingTypeTest ( n , _, _, result ) and
4446 result .( Expr ) .isNthChildOf ( n , index )
4547 or
4648 result .( Stmt ) .isNthChildOf ( n , index )
4749 or
4850 result = n .( SwitchCase ) .getRuleExpression ( ) and index = - 1
51+ or
52+ isBindingTypeTest ( _, n , _, result ) and
53+ index = 0
54+ or
55+ isBindingTypeTest ( n , result , index , _)
4956 }
5057
5158 Callable getEnclosingCallable ( AstNode node ) {
@@ -177,10 +184,7 @@ private module Ast implements AstSig<Location> {
177184
178185 class Case extends AstNode instanceof J:: SwitchCase {
179186 /** Gets the pattern being matched by this case at the specified (zero-based) `index`. */
180- AstNode getPattern ( int index ) {
181- result = this .( PatternCase ) .getPattern ( index ) or
182- result = this .( ConstCase ) .getValue ( index )
183- }
187+ AstNode getPattern ( int index ) { result = getSwitchCasePattern ( this , index ) }
184188
185189 /** Gets the guard expression of this case, if any. */
186190 Expr getGuard ( ) { result = this .( PatternCase ) .getGuard ( ) }
@@ -241,12 +245,45 @@ private module Ast implements AstSig<Location> {
241245 boolean getValue ( ) { result = this .getBooleanValue ( ) }
242246 }
243247
248+ final class Pattern = PatternImpl ;
249+
250+ abstract private class PatternImpl extends AstNode { }
251+
252+ private class NonBindingPattern extends PatternImpl {
253+ NonBindingPattern ( ) {
254+ isBindingTypeTest ( _, this , _, _)
255+ or
256+ this instanceof RecordPatternExpr
257+ }
258+ }
259+
260+ class AndPattern extends Pattern {
261+ AndPattern ( ) { none ( ) }
262+ }
263+
264+ class OrPattern extends Pattern {
265+ OrPattern ( ) { none ( ) }
266+ }
267+
268+ class NotPattern extends Pattern {
269+ NotPattern ( ) { none ( ) }
270+
271+ Pattern getNegatedPattern ( ) { none ( ) }
272+ }
273+
274+ class BindingPattern extends PatternImpl {
275+ BindingPattern ( ) {
276+ isBindingTypeTest ( _, _, _, this )
277+ or
278+ // needed for cases like `A(var x)` where there is no type test
279+ this = any ( RecordPatternExpr rpe ) .getSubPattern ( _) .( LocalVariableDeclExpr )
280+ }
281+ }
282+
244283 final private class FinalInstanceOfExpr = J:: InstanceOfExpr ;
245284
246285 class PatternMatchExpr extends FinalInstanceOfExpr {
247- PatternMatchExpr ( ) { this .isPattern ( ) }
248-
249- AstNode getPattern ( ) { result = super .getPattern ( ) }
286+ AstNode getPattern ( ) { result = getInstanceOfExprPattern ( this ) }
250287 }
251288}
252289
@@ -532,6 +569,17 @@ private module Input implements InputSig1, InputSig2 {
532569 c .( J:: PatternCase ) .getAPattern ( ) .getType ( ) instanceof TypeObject
533570 }
534571
572+ predicate patternIsExhaustive ( Ast:: Pattern pattern ) {
573+ pattern .( TypeAccess ) .getType ( ) instanceof TypeObject
574+ or
575+ exists ( PatternExpr sub | any ( RecordPatternExpr rpe ) .subPatternIsUnrestricted ( sub , _) |
576+ isBindingTypeTest ( _, pattern , _, sub )
577+ or
578+ not sub instanceof LocalVariableDeclExpr and
579+ pattern = sub
580+ )
581+ }
582+
535583 private newtype TLabel =
536584 TJavaLabel ( string l ) { exists ( LabeledStmt lbl | l = lbl .getLabel ( ) ) } or
537585 TYield ( )
@@ -619,10 +667,10 @@ private module Input implements InputSig1, InputSig2 {
619667 n1 .isBefore ( assertstmt ) and
620668 n2 .isBefore ( assertstmt .getExpr ( ) )
621669 or
622- n1 .isAfterValue ( assertstmt .getExpr ( ) , any ( BooleanSuccessor s | s . getValue ( ) = true ) ) and
670+ n1 .isAfterTrue ( assertstmt .getExpr ( ) ) and
623671 n2 .isAfter ( assertstmt )
624672 or
625- n1 .isAfterValue ( assertstmt .getExpr ( ) , any ( BooleanSuccessor s | s . getValue ( ) = false ) ) and
673+ n1 .isAfterFalse ( assertstmt .getExpr ( ) ) and
626674 (
627675 n2 .isBefore ( assertstmt .getMessage ( ) )
628676 or
0 commit comments