@@ -86,6 +86,41 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
8686 AstNode getAPattern ( ) { super .getPattern ( ) = result }
8787 }
8888
89+ final private class FinalIsExpr = CS:: IsExpr ;
90+
91+ class Pattern = CS:: PatternExpr ;
92+
93+ class AndPattern = CS:: AndPatternExpr ;
94+
95+ class OrPattern = CS:: OrPatternExpr ;
96+
97+ class NotPattern extends CS:: NotPatternExpr {
98+ Pattern getNegatedPattern ( ) { result = this .getPattern ( ) }
99+ }
100+
101+ class PatternMatchExpr extends FinalIsExpr {
102+ AstNode getPattern ( ) { result = super .getPattern ( ) }
103+ }
104+
105+ predicate nonDeconstructingSubPattern ( AstNode pattern , AstNode subPattern ) {
106+ subPattern = pattern .( AndPatternExpr ) .getAnOperand ( )
107+ or
108+ subPattern = pattern .( OrPatternExpr ) .getAnOperand ( )
109+ or
110+ subPattern = pattern .( NotPatternExpr ) .getPattern ( )
111+ or
112+ pattern =
113+ any ( RecursivePatternExpr rpe |
114+ subPattern =
115+ [
116+ rpe .getTypeAccess ( ) .( AstNode ) , //
117+ rpe .getVariableDeclExpr ( ) , //
118+ rpe .getPositionalPatterns ( ) , //
119+ rpe .getPropertyPatterns ( )
120+ ]
121+ )
122+ }
123+
89124 abstract private class BinExpr extends BinaryOperation { }
90125
91126 class AndExpr extends BinExpr {
@@ -125,18 +160,6 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
125160 or
126161 ct .getComparisonKind ( ) .isInequality ( ) and polarity = false
127162 )
128- or
129- exists ( IsExpr ie , PatternExpr pat |
130- ie = eqtest and
131- ie .getExpr ( ) = left and
132- ie .getPattern ( ) = pat
133- |
134- right = pat .( ConstantPatternExpr ) and
135- polarity = true
136- or
137- right = pat .( NotPatternExpr ) .getPattern ( ) .( ConstantPatternExpr ) and
138- polarity = false
139- )
140163 }
141164
142165 class ConditionalExpr = CS:: ConditionalExpr ;
@@ -209,38 +232,15 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
209232 isNull = false
210233 )
211234 or
212- // TODO: Remove this superfluous case once CFG changes for IsExpr lands.
213- guard =
214- any ( PatternMatch pm |
215- e instanceof DereferenceableExpr and
216- e = pm .getExpr ( ) and
217- (
218- val .asBooleanValue ( ) .booleanNot ( ) = patternMatchesNull ( pm .getPattern ( ) ) and
219- isNull = false
220- or
221- exists ( TypePatternExpr tpe |
222- // E.g. `x is string` where `x` has type `string`
223- typePattern ( guard , tpe , tpe .getCheckedType ( ) ) and
224- val .asBooleanValue ( ) = false and
225- isNull = true
226- )
227- )
228- )
229- or
235+ // E.g. `x is string` where `x` has type `string`
230236 exists ( PatternMatch pm |
231- guard = pm .getPattern ( ) and
232237 e instanceof DereferenceableExpr and
233- e = pm .getExpr ( )
234- |
235- val .isMatch ( patternMatchesNull ( pm .getPattern ( ) ) .booleanNot ( ) ) and
236- isNull = false
237- or
238- exists ( TypePatternExpr tpe |
239- // E.g. `x is string` where `x` has type `string`
240- typePattern ( pm , tpe , tpe .getCheckedType ( ) ) and
241- val .isMatch ( false ) and
242- isNull = true
243- )
238+ e = pm .getExpr ( ) and
239+ GuardsInput:: nonDeconstructingSubPattern * ( pm .getPattern ( ) , guard ) and
240+ guard .( TypePatternExpr ) .getCheckedType ( ) = e .getType ( ) and
241+ not guard .( VariablePatternExpr ) .isImplicitlyTyped ( ) and
242+ val .isMatch ( false ) and
243+ isNull = true
244244 )
245245 or
246246 e .( DereferenceableExpr ) .hasNullableType ( ) and
@@ -355,35 +355,6 @@ deprecated module AbstractValues {
355355 }
356356}
357357
358- /** Gets the value resulting from matching `null` against `pat`. */
359- private boolean patternMatchesNull ( PatternExpr pat ) {
360- pat instanceof NullLiteral and result = true
361- or
362- not pat instanceof NullLiteral and
363- not pat instanceof NotPatternExpr and
364- not pat instanceof OrPatternExpr and
365- not pat instanceof AndPatternExpr and
366- result = false
367- or
368- result = patternMatchesNull ( pat .( NotPatternExpr ) .getPattern ( ) ) .booleanNot ( )
369- or
370- exists ( OrPatternExpr ope | pat = ope |
371- result =
372- patternMatchesNull ( ope .getLeftOperand ( ) ) .booleanOr ( patternMatchesNull ( ope .getRightOperand ( ) ) )
373- )
374- or
375- exists ( AndPatternExpr ape | pat = ape |
376- result =
377- patternMatchesNull ( ape .getLeftOperand ( ) ) .booleanAnd ( patternMatchesNull ( ape .getRightOperand ( ) ) )
378- )
379- }
380-
381- pragma [ nomagic]
382- private predicate typePattern ( PatternMatch pm , TypePatternExpr tpe , Type t ) {
383- tpe = pm .getPattern ( ) and
384- t = pm .getExpr ( ) .getType ( )
385- }
386-
387358pragma [ nomagic]
388359private predicate dereferenceableExpr ( Expr e , boolean isNullableType ) {
389360 exists ( Type t | t = e .getType ( ) |
@@ -884,6 +855,16 @@ module Internal {
884855 e instanceof SuppressNullableWarningExpr
885856 or
886857 e .stripCasts ( ) .getType ( ) = any ( ValueType t | not t instanceof NullableType )
858+ or
859+ e =
860+ any ( PatternExpr pat |
861+ not pat instanceof NullLiteral and
862+ not pat instanceof DiscardPatternExpr and
863+ not pat .( VariablePatternExpr ) .isImplicitlyTyped ( ) and
864+ not pat instanceof NotPatternExpr and
865+ not pat instanceof OrPatternExpr and
866+ not pat instanceof AndPatternExpr
867+ )
887868 }
888869
889870 /** Holds if expression `e2` is a non-`null` value whenever `e1` is. */
0 commit comments