Skip to content

Commit

Permalink
Remove unnecessary continue/return hint should handle rule cases and …
Browse files Browse the repository at this point in the history
…switch expressions.
  • Loading branch information
lahodaj committed Nov 15, 2024
1 parent 9872423 commit a4eeab9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,38 @@ private static ErrorDescription unnecessaryReturnContinue(HintContext ctx, Tree
}
case BLOCK: statements = ((BlockTree) tp.getLeaf()).getStatements(); break;
case CASE: {
if (tp.getParentPath().getLeaf().getKind() == Kind.SWITCH) {
List<? extends CaseTree> cases = ((SwitchTree) tp.getParentPath().getLeaf()).getCases();
List<StatementTree> locStatements = new ArrayList<>();

for (int i = cases.indexOf(tp.getLeaf()); i < cases.size(); i++) {
List<? extends StatementTree> list = cases.get(i).getStatements();
if (list != null) {
locStatements.addAll(list);
switch (tp.getParentPath().getLeaf().getKind()) {
case SWITCH -> {
CaseTree ct = (CaseTree) tp.getLeaf();
if (ct.getCaseKind() == CaseTree.CaseKind.RULE) {
if (ExpressionTree.class.isAssignableFrom(ct.getBody().getKind().asInterface())) {
//TODO: anything that can be done here?
return null;
} else {
statements = List.of((StatementTree) ct.getBody());
}
} else {
List<? extends CaseTree> cases = ((SwitchTree) tp.getParentPath().getLeaf()).getCases();
List<StatementTree> locStatements = new ArrayList<>();

for (int i = cases.indexOf(tp.getLeaf()); i < cases.size(); i++) {
List<? extends StatementTree> list = cases.get(i).getStatements();
if (list != null) {
locStatements.addAll(list);
}
}

statements = locStatements;
}
}

statements = locStatements;
} else {
//???
statements = ((CaseTree) tp.getLeaf()).getStatements();
case SWITCH_EXPRESSION -> {
//jumps out of switch expressions are illegal, ignore:
return null;
}
default -> {
//???
statements = ((CaseTree) tp.getLeaf()).getStatements();
}
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,64 @@ public void testBreakWithLabelInSwitch() throws Exception {
.run(RemoveUnnecessary.class)
.assertWarnings();
}

public void testRuleSwitch1() throws Exception {
HintTest.create()
.input("""
package test;
public class Test {
public void testContinue(int i) {
switch (i) {
case 0 -> { return ; }
case 1 -> { return ; }
default -> { }
}
System.err.println("not end");
}
}
""")
.sourceLevel("17")
.run(RemoveUnnecessary.class)
.assertWarnings();
}

public void testRuleSwitch2() throws Exception {
HintTest.create()
.input("""
package test;
public class Test {
public void testContinue(int i) {
switch (i) {
case 0 -> { return ; }
case 1 -> { return ; }
default -> { }
}
}
}
""")
.sourceLevel("17")
.run(RemoveUnnecessary.class)
.assertWarnings("4:24-4:32:verifier:Unnecessary return statement",
"5:24-5:32:verifier:Unnecessary return statement");
}

public void testRuleSwitchExpression() throws Exception {
HintTest.create()
.input("""
package test;
public class Test {
public int testContinue(int i) {
return switch (i) {
case 0 -> { return 0; } //illegal
case 1 -> { return 0; } //illegal
default -> -1;
};
}
}
""",
false)
.sourceLevel("17")
.run(RemoveUnnecessary.class)
.assertWarnings();
}
}

0 comments on commit a4eeab9

Please sign in to comment.