Skip to content

Commit a58f2b0

Browse files
committed
fix 'continue not in a loop' inspector
Previously the inspector would look for the presence of a GoForStatement parent of a continue statement. This did not take into account the presence of 'go' or 'defer' calls, e.g. for { defer func() { continue // no error was raised, should be an error }() } The discussion at #2024 reminded me that the continue inspection suffers from this problem.
1 parent f206433 commit a58f2b0

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/com/goide/inspections/GoContinueNotInLoopInspection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.goide.psi.GoContinueStatement;
2020
import com.goide.psi.GoForStatement;
21+
import com.goide.psi.GoFunctionLit;
2122
import com.goide.psi.GoVisitor;
2223
import com.goide.psi.impl.GoElementFactory;
2324
import com.intellij.codeInspection.*;
@@ -39,9 +40,9 @@ protected GoVisitor buildGoVisitor(@NotNull final ProblemsHolder holder, @NotNul
3940
return new GoVisitor() {
4041
@Override
4142
public void visitContinueStatement(@NotNull GoContinueStatement o) {
42-
if (PsiTreeUtil.getParentOfType(o, GoForStatement.class) == null) {
43-
holder.registerProblem(o, "Continue statement not inside a for loop.", ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
44-
new ReplaceWithReturnQuickFix());
43+
if (!(PsiTreeUtil.getParentOfType(o, GoForStatement.class, GoFunctionLit.class) instanceof GoForStatement)) {
44+
holder.registerProblem(o, "Continue statement not inside a for loop.",
45+
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithReturnQuickFix());
4546
}
4647
}
4748
};

testData/highlighting/continue.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import "fmt"
55
func _() {
66
for i := 0; i < 10; i++ {
77
fmt.Printf("%d\n", i)
8+
f := func() {
9+
<error>continue</error>
10+
}
11+
f()
812
continue
913
}
1014

@@ -13,4 +17,16 @@ func _() {
1317
if 1 > 0 {
1418
<error>continue</error>
1519
}
20+
21+
for i := 0; i < 10; i++ {
22+
defer func() {
23+
<error>continue</error>
24+
}()
25+
26+
go func() {
27+
<error>continue</error>
28+
}()
29+
30+
continue
31+
}
1632
}

0 commit comments

Comments
 (0)