Skip to content

Commit

Permalink
refactor: replace simple switch with if-else
Browse files Browse the repository at this point in the history
`switch` statements that have only two arms can be better represented as `if` statements.
  • Loading branch information
deepsource-autofix[bot] authored Jan 20, 2025
1 parent db25dee commit a1741d0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
78 changes: 58 additions & 20 deletions spoon-smpl/src/main/java/spoon/smpl/FormulaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,9 @@ private Formula getDotsPreGuard(ControlFlowNode dotsNode, List<String> quantifie
quantifiedMetavars = shallowCopy(quantifiedMetavars);
List<ControlFlowNode> prevNodes = dotsNode.prev();

switch (prevNodes.size()) {
case 1:
ControlFlowNode prevNode = prevNodes.get(0);

switch (prevNode.getKind()) {
if (prevNodes.size() == 1) {
ControlFlowNode prevNode = prevNodes.get(0);
switch (prevNode.getKind()) {
case STATEMENT:
if (SmPLMethodCFG.isMethodHeaderNode(prevNode)) {
return null;
Expand All @@ -431,13 +429,45 @@ private Formula getDotsPreGuard(ControlFlowNode dotsNode, List<String> quantifie
return compileFormulaInner(prevNode, Collections.singletonList(dotsNode), quantifiedMetavars);

case BLOCK_BEGIN:
switch (prevNode.prev().size()) {
case 1:
return compileFormulaInner(prevNode.prev().get(0), prevNode.prev().get(0).next(), quantifiedMetavars);
if (prevNode.prev().size() == 1) {
return compileFormulaInner(prevNode.prev().get(0), prevNode.prev().get(0).next(), quantifiedMetavars);
}

throw new NotImplementedException("preGuard not implemented for BLOCK_BEGIN with " + Integer.toString(prevNode.prev().size()) + " predecessors");



case CONVERGE:
// FIXME: relies on implementation details in spoon-control-flow
ControlFlowNode branchNode = prevNode.getParent().findNodeById(prevNode.getId() - 1);

default:
throw new NotImplementedException("preGuard not implemented for BLOCK_BEGIN with " + Integer.toString(prevNode.prev().size()) + " predecessors");
if (SmPLJavaDSL.isBeginDisjunction(branchNode.getStatement().getParent())) {
// TODO: figure out if a disjunction should generate a guard
return null;
} else {
return compileFormulaInner(branchNode, branchNode.next(), quantifiedMetavars);
}
default:
throw new NotImplementedException("preGuard not implemented for " + prevNode.getKind().toString() + " single predecessor");
}
}

ControlFlowNode prevNode = prevNodes.get(0);
switch (prevNode.getKind()) {
case STATEMENT:
if (SmPLMethodCFG.isMethodHeaderNode(prevNode)) {
return null;
}

return compileFormulaInner(prevNode, Collections.singletonList(dotsNode), quantifiedMetavars);

case BLOCK_BEGIN:
if (prevNode.prev().size() == 1) {
return compileFormulaInner(prevNode.prev().get(0), prevNode.prev().get(0).next(), quantifiedMetavars);
}

throw new NotImplementedException("preGuard not implemented for BLOCK_BEGIN with " + Integer.toString(prevNode.prev().size()) + " predecessors");



case CONVERGE:
Expand All @@ -454,9 +484,6 @@ private Formula getDotsPreGuard(ControlFlowNode dotsNode, List<String> quantifie
throw new NotImplementedException("preGuard not implemented for " + prevNode.getKind().toString() + " single predecessor");
}

default:
throw new NotImplementedException("preGuard not implemented for " + prevNodes.size() + " predecessors");
}
}

/**
Expand All @@ -476,11 +503,25 @@ private Formula getDotsPostGuard(ControlFlowNode dotsNode, List<String> quantifi
nextNodes = dotsNode.getParent().findNodeById(dotsNode.getId() + 1).next();
}

switch (nextNodes.size()) {
case 1:
ControlFlowNode nextNode = nextNodes.get(0);
if (nextNodes.size() == 1) {
ControlFlowNode nextNode = nextNodes.get(0);
switch (nextNode.getKind()) {
case STATEMENT:
return compileFormulaInner(nextNode, nextNode.next(), quantifiedMetavars);

case CONVERGE:
return null;

case EXIT:
return null;

switch (nextNode.getKind()) {
default:
throw new NotImplementedException("postGuard not implemented for " + nextNode.getKind().toString() + " single successor");
}
}

ControlFlowNode nextNode = nextNodes.get(0);
switch (nextNode.getKind()) {
case STATEMENT:
return compileFormulaInner(nextNode, nextNode.next(), quantifiedMetavars);

Expand All @@ -494,9 +535,6 @@ private Formula getDotsPostGuard(ControlFlowNode dotsNode, List<String> quantifi
throw new NotImplementedException("postGuard not implemented for " + nextNode.getKind().toString() + " single successor");
}

default:
throw new NotImplementedException("postGuard not implemented for " + nextNodes.size() + " successors");
}
}

/**
Expand Down
13 changes: 5 additions & 8 deletions spoon-smpl/src/main/java/spoon/smpl/SmPLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,11 @@ public static SmPLRule compile(String source, CtClass<?> ast, AnchoredOperations
String constraintType = ((CtLiteral<?>) invocation.getArguments().get(0)).getValue().toString();
String constraintValue = ((CtLiteral<?>) invocation.getArguments().get(1)).getValue().toString();

switch (constraintType) {
case "regex-match":
metavars.put(currentVarName, new RegexConstraint(constraintValue, metavars.get(currentVarName)));
break;

default:
throw new IllegalArgumentException("unknown constraint type " + constraintType);
}
if (constraintType == "regex-match") {
metavars.put(currentVarName, new RegexConstraint(constraintValue, metavars.get(currentVarName)));
} else {
throw new IllegalArgumentException("unknown constraint type " + constraintType);
}


} else {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/spoon/reflect/visitor/CommentHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ static void printComment(PrinterHelper printer, CtComment comment) {
break;
}
// content
switch (commentType) {
case INLINE:
printer.write(content);
break;
default:
// per line suffix
printCommentContent(printer, comment, s -> { return (" * " + s).replaceAll(" *$", ""); });
}
if (commentType == INLINE) {
printer.write(content);
} else {
printCommentContent(printer, comment, s -> { return (" * " + s).replaceAll(" *$", ""); });
}
// suffix
switch (commentType) {
case BLOCK:
Expand Down

0 comments on commit a1741d0

Please sign in to comment.