Make DefaultCase include NullDefaultCase

This commit is contained in:
Chris Smowton
2023-11-16 14:32:06 +00:00
parent d2ff1baff0
commit 6e868d21bd
4 changed files with 13 additions and 21 deletions

View File

@@ -1535,7 +1535,7 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr {
*
* Note this may be `default` or `case null, default`.
*/
SwitchCase getDefaultCase() { result = this.getACase() and result.hasDefaultLabel() }
DefaultCase getDefaultCase() { result = this.getACase() }
/** Gets the expression of this `switch` expression. */
Expr getExpr() { result.getParent() = this }
@@ -1738,9 +1738,7 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
/** A local variable declaration that occurs within a record pattern. */
class RecordBindingVariableExpr extends LocalVariableDeclExpr {
RecordBindingVariableExpr() {
this.getParent() instanceof RecordPatternExpr
}
RecordBindingVariableExpr() { this.getParent() instanceof RecordPatternExpr }
}
/** An update of a variable or an initialization of the variable. */

View File

@@ -408,7 +408,7 @@ class SwitchStmt extends Stmt, @switchstmt {
*
* Note this may be `default` or `case null, default`.
*/
SwitchCase getDefaultCase() { result = this.getACase() and result.hasDefaultLabel() }
DefaultCase getDefaultCase() { result = this.getACase() }
/** Gets the expression of this `switch` statement. */
Expr getExpr() { result.getParent() = this }
@@ -492,12 +492,6 @@ class SwitchCase extends Stmt, @case {
Stmt getRuleStatementOrExpressionStatement() {
result.getParent() = this and result.getIndex() = -1
}
/**
* Holds if this case statement includes the default label, i.e. it is either `default`
* or `case null, default`.
*/
predicate hasDefaultLabel() { this instanceof DefaultCase or this instanceof NullDefaultCase }
}
/**
@@ -553,12 +547,14 @@ class PatternCase extends SwitchCase {
}
/**
* A `default` case of a `switch` statement.
*
* Note this does not include `case null, default` -- for that, see `NullDefaultCase`.
* A `default` or `case null, default` case of a `switch` statement or expression.
*/
class DefaultCase extends SwitchCase {
DefaultCase() { not exists(Expr e | e.getParent() = this | e.getIndex() >= 0) }
DefaultCase() {
isNullDefaultCase(this)
or
not exists(Expr e | e.getParent() = this | e.getIndex() >= 0)
}
override string pp() { result = "default" }
@@ -570,7 +566,7 @@ class DefaultCase extends SwitchCase {
}
/** A `case null, default` statement of a `switch` statement or expression. */
class NullDefaultCase extends SwitchCase {
class NullDefaultCase extends DefaultCase {
NullDefaultCase() { isNullDefaultCase(this) }
override string pp() { result = "case null, default" }

View File

@@ -55,11 +55,11 @@ predicate implies_v1(Guard g1, boolean b1, Guard g2, boolean b2) {
)
)
or
exists(SwitchCase sc | g1 = sc and sc.hasDefaultLabel() |
exists(DefaultCase sc | g1 = sc |
sc.getSwitch().getAConstCase() = g2 and b1 = true and b2 = false
)
or
exists(SwitchCase sc | g1 = sc and sc.hasDefaultLabel() |
exists(DefaultCase sc | g1 = sc |
sc.getSwitchExpr().getAConstCase() = g2 and b1 = true and b2 = false
)
or

View File

@@ -78,9 +78,7 @@ private predicate branchingSwitchCase(ConstCase sc) {
}
private predicate defaultFallThrough(ConstCase sc) {
exists(SwitchCase default | default.hasDefaultLabel() |
default.(ControlFlowNode).getASuccessor() = sc
)
exists(DefaultCase default | default.(ControlFlowNode).getASuccessor() = sc)
or
defaultFallThrough(sc.(ControlFlowNode).getAPredecessor())
}