Replace getDefaultOrNullDefaultCase with getDefaultCase

This commit is contained in:
Chris Smowton
2023-11-16 14:23:56 +00:00
parent 0bb051e08c
commit d2ff1baff0
7 changed files with 17 additions and 15 deletions

View File

@@ -14,5 +14,5 @@ where
switch.getExpr().getType() = enum and switch.getExpr().getType() = enum and
missing.getDeclaringType() = enum and missing.getDeclaringType() = enum and
not switch.getAConstCase().getValue() = missing.getAnAccess() and not switch.getAConstCase().getValue() = missing.getAnAccess() and
not exists(switch.getDefaultOrNullDefaultCase()) not exists(switch.getDefaultCase())
select switch select switch

View File

@@ -923,7 +923,7 @@ private module ControlFlowGraphImpl {
completion = NormalCompletion() completion = NormalCompletion()
or or
// if no default case exists, then normal completion of the expression may terminate the switch // if no default case exists, then normal completion of the expression may terminate the switch
not exists(switch.getDefaultOrNullDefaultCase()) and not exists(switch.getDefaultCase()) and
last(switch.getExpr(), last, completion) and last(switch.getExpr(), last, completion) and
completion = NormalCompletion() completion = NormalCompletion()
) )

View File

@@ -1530,11 +1530,12 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr {
/** Gets a (non-default) pattern `case` of this `switch` expression. */ /** Gets a (non-default) pattern `case` of this `switch` expression. */
PatternCase getAPatternCase() { result = this.getACase() } PatternCase getAPatternCase() { result = this.getACase() }
/** Gets the `default` case of this switch expression, if any. */ /**
DefaultCase getDefaultCase() { result = this.getACase() } * Gets the `default` case of this switch statement, if any.
*
/** Gets the `default` or `case null, default` case of this switch statement, if any. */ * Note this may be `default` or `case null, default`.
SwitchCase getDefaultOrNullDefaultCase() { result = this.getACase() and result.hasDefaultLabel() } */
SwitchCase getDefaultCase() { result = this.getACase() and result.hasDefaultLabel() }
/** Gets the expression of this `switch` expression. */ /** Gets the expression of this `switch` expression. */
Expr getExpr() { result.getParent() = this } Expr getExpr() { result.getParent() = this }

View File

@@ -403,11 +403,12 @@ class SwitchStmt extends Stmt, @switchstmt {
/** Gets a (non-default) pattern `case` of this `switch` statement. */ /** Gets a (non-default) pattern `case` of this `switch` statement. */
PatternCase getAPatternCase() { result = this.getACase() } PatternCase getAPatternCase() { result = this.getACase() }
/** Gets the `default` case of this switch statement, if any. */ /**
DefaultCase getDefaultCase() { result = this.getACase() } * Gets the `default` case of this switch statement, if any.
*
/** Gets the `default` or `case null, default` case of this switch statement, if any. */ * Note this may be `default` or `case null, default`.
SwitchCase getDefaultOrNullDefaultCase() { result = this.getACase() and result.hasDefaultLabel() } */
SwitchCase getDefaultCase() { result = this.getACase() and result.hasDefaultLabel() }
/** Gets the expression of this `switch` statement. */ /** Gets the expression of this `switch` statement. */
Expr getExpr() { result.getParent() = this } Expr getExpr() { result.getParent() = this }

View File

@@ -174,7 +174,7 @@ class ConstSwitchStmt extends SwitchStmt {
exists(this.getExpr().(ConstantExpr).getIntValue()) and exists(this.getExpr().(ConstantExpr).getIntValue()) and
if exists(this.getMatchingConstCase()) if exists(this.getMatchingConstCase())
then result = this.getMatchingConstCase() then result = this.getMatchingConstCase()
else result = this.getDefaultOrNullDefaultCase() else result = this.getDefaultCase()
} }
/** /**

View File

@@ -15,5 +15,5 @@ import java
from SwitchStmt switch from SwitchStmt switch
where where
not switch.getExpr().getType() instanceof EnumType and not switch.getExpr().getType() instanceof EnumType and
not exists(switch.getDefaultOrNullDefaultCase()) not exists(switch.getDefaultCase())
select switch, "Switch statement does not have a default case." select switch, "Switch statement does not have a default case."

View File

@@ -17,6 +17,6 @@ from SwitchStmt switch, EnumType enum, EnumConstant missing
where where
switch.getExpr().getType() = enum and switch.getExpr().getType() = enum and
missing.getDeclaringType() = enum and missing.getDeclaringType() = enum and
not exists(switch.getDefaultOrNullDefaultCase()) and not exists(switch.getDefaultCase()) and
not switch.getAConstCase().getValue() = missing.getAnAccess() not switch.getAConstCase().getValue() = missing.getAnAccess()
select switch, "Switch statement does not have a case for $@.", missing, missing.getName() select switch, "Switch statement does not have a case for $@.", missing, missing.getName()