Skip to content

Commit 7845df6

Browse files
committed
Initial "address of a for loop variable" inspection
1 parent 572315e commit 7845df6

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

resources/META-INF/plugin.xml

+3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@
236236
<localInspection language="go" displayName="Printf/Logf placeholder handler" groupPath="Go"
237237
groupName="Probable bug" enabledByDefault="true" level="WARNING"
238238
implementationClass="com.goide.inspections.GoPlaceholderCountInspection"/>
239+
<localInspection language="go" displayName="Obtaining address of a for loop variable" groupPath="Go"
240+
groupName="Probable bugs" enabledByDefault="true" level="WEAK WARNING"
241+
implementationClass="com.goide.inspections.GoAddressOfLoopVariableInspection"/>
239242
<!-- /probable bugs -->
240243

241244
<!-- control flow issues -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
~ Copyright 2013-2016 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+
Checks for obtaining addresses of for loop variables.
20+
Address of a for loop variable remains the same on all iterations of a loop, hence it's probably a bug.
21+
</body>
22+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2013-2016 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.*;
20+
import com.intellij.codeInspection.LocalInspectionToolSession;
21+
import com.intellij.codeInspection.ProblemsHolder;
22+
import com.intellij.psi.PsiElement;
23+
import com.intellij.util.ObjectUtils;
24+
import org.jetbrains.annotations.NotNull;
25+
26+
public class GoAddressOfLoopVariableInspection extends GoInspectionBase {
27+
@NotNull
28+
@Override
29+
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
30+
return new GoVisitor() {
31+
@Override
32+
public void visitUnaryExpr(@NotNull GoUnaryExpr o) {
33+
if (o.getBitAnd() == null) {
34+
return;
35+
}
36+
37+
//TODO improve by supporting references to fields of struct variables
38+
GoReferenceExpression refExpr = ObjectUtils.tryCast(o.getExpression(), GoReferenceExpression.class);
39+
if (refExpr == null || refExpr.getQualifier() != null) {
40+
return;
41+
}
42+
43+
GoVarDefinition varDefinition = ObjectUtils.tryCast(refExpr.getReference().resolve(), GoVarDefinition.class);
44+
PsiElement parent = varDefinition != null ? varDefinition.getParent() : null;
45+
if (!(parent instanceof GoRangeClause)) {
46+
return;
47+
}
48+
49+
holder.registerProblem(o, "Suspicious: obtaining address of a for loop variable");
50+
}
51+
};
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
func main() {
4+
for _, i := range []int{1, 2, 3} {
5+
println(<weak_warning descr="Suspicious: obtaining address of a for loop variable">&i</weak_warning>)
6+
}
7+
}

tests/com/goide/inspections/GoHighlightingTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public void setUp() throws Exception {
6666
GoPlaceholderCountInspection.class,
6767
GoEmbeddedInterfacePointerInspection.class,
6868
GoStructInitializationInspection.class,
69-
GoMethodOnNonLocalTypeInspection.class
69+
GoMethodOnNonLocalTypeInspection.class,
70+
GoAddressOfLoopVariableInspection.class
7071
);
7172
}
7273

@@ -350,6 +351,8 @@ public void testUnitializedStructInitialization() {
350351
doWeakTest();
351352
}
352353

354+
public void testAddressOfLoopVariable() { doWeakTest(); }
355+
353356
private long doWeakTest() {return myFixture.testHighlighting(true, false, true, getTestName(true) + ".go");}
354357

355358
public void testDoNotHighlightCommentOfMainPackage() {

0 commit comments

Comments
 (0)