Skip to content

Commit 4c669ec

Browse files
committed
Merge pull request #2004 from sjamesr/add_continue_outside_loop_inspection
add an inspection for 'continue' outside loop
2 parents 1a13cc8 + a7a412d commit 4c669ec

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@
263263
<localInspection language="go" displayName="Self import" groupPath="Go"
264264
groupName="General" enabledByDefault="true" level="ERROR"
265265
implementationClass="com.goide.inspections.GoSelfImportInspection"/>
266+
<localInspection language="go" displayName="Continue statement outside for loop" groupPath="Go"
267+
groupName="General" enabledByDefault="true" level="ERROR"
268+
implementationClass="com.goide.inspections.GoContinueNotInLoopInspection"/>
266269
<!-- /general -->
267270
</extensions>
268271
<actions>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
~ Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Florin Patan
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<html>
18+
<body>
19+
Ensures that continue statements only occur inside for loops.
20+
</body>
21+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Florin Patan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.goide.inspections;
18+
19+
import com.goide.psi.GoContinueStatement;
20+
import com.goide.psi.GoForStatement;
21+
import com.goide.psi.GoVisitor;
22+
import com.intellij.codeInspection.LocalInspectionToolSession;
23+
import com.intellij.codeInspection.ProblemHighlightType;
24+
import com.intellij.codeInspection.ProblemsHolder;
25+
import com.intellij.psi.util.PsiTreeUtil;
26+
import org.jetbrains.annotations.NotNull;
27+
28+
/**
29+
* Raises an error when a 'continue' statement is found outside of a for loop.
30+
*/
31+
public class GoContinueNotInLoopInspection extends GoInspectionBase {
32+
@NotNull
33+
@Override
34+
protected GoVisitor buildGoVisitor(@NotNull final ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
35+
return new GoVisitor() {
36+
@Override
37+
public void visitContinueStatement(@NotNull GoContinueStatement o) {
38+
if (PsiTreeUtil.getParentOfType(o.getContinue(), GoForStatement.class) == null) {
39+
holder
40+
.registerProblem(o.getContinue(), "Continue statement not inside a for loop.", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
41+
}
42+
}
43+
};
44+
}
45+
}

testData/highlighting/continue.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func _() {
6+
for i := 0; i < 10; i++ {
7+
fmt.Printf("%d\n", i)
8+
continue
9+
}
10+
11+
<error>continue</error>
12+
13+
if 1 > 0 {
14+
<error>continue</error>
15+
}
16+
}

tests/com/goide/inspections/GoHighlightingTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public void setUp() throws Exception {
5656
GoTestSignaturesInspection.class,
5757
GoAssignmentNilWithoutExplicitTypeInspection.class,
5858
GoRedeclareImportAsFunctionInspection.class,
59-
GoStructTagInspection.class
59+
GoStructTagInspection.class,
60+
GoContinueNotInLoopInspection.class
6061
);
6162
}
6263

@@ -114,7 +115,8 @@ protected boolean isWriteActionRequired() {
114115
public void testVarToImport() { doTest(); }
115116
public void testCgotest() { doTest(); }
116117
public void testRedeclaredImportAsFunction(){ doTest(); }
117-
public void testStructTags() { doTest(); }
118+
public void testStructTags() { doTest(); }
119+
public void testContinue() { doTest(); }
118120

119121
public void testRelativeImportIgnoringDirectories() throws IOException {
120122
myFixture.getTempDirFixture().findOrCreateDir("to_import/testdata");

0 commit comments

Comments
 (0)