Skip to content

WIP: New inspection: Obtaining address of a for loop variable #2422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@
<localInspection language="go" displayName="Printf/Logf placeholder handler" groupPath="Go"
groupName="Probable bug" enabledByDefault="true" level="WARNING"
implementationClass="com.goide.inspections.GoPlaceholderCountInspection"/>
<localInspection language="go" displayName="Obtaining address of a for loop variable" groupPath="Go"
groupName="Probable bugs" enabledByDefault="true" level="WEAK WARNING"
implementationClass="com.goide.inspections.GoAddressOfLoopVariableInspection"/>
<!-- /probable bugs -->

<!-- control flow issues -->
Expand Down
22 changes: 22 additions & 0 deletions resources/inspectionDescriptions/GoAddressOfLoopVariable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
~ Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
Checks for obtaining addresses of for loop variables.
Address of a for loop variable remains the same on all iterations of a loop, hence it's probably a bug.
</body>
</html>
53 changes: 53 additions & 0 deletions src/com/goide/inspections/GoAddressOfLoopVariableInspection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.goide.inspections;

import com.goide.psi.*;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;

public class GoAddressOfLoopVariableInspection extends GoInspectionBase {
@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
return new GoVisitor() {
@Override
public void visitUnaryExpr(@NotNull GoUnaryExpr o) {
if (o.getBitAnd() == null) {
return;
}

//TODO improve by supporting references to fields of struct variables
GoReferenceExpression refExpr = ObjectUtils.tryCast(o.getExpression(), GoReferenceExpression.class);
if (refExpr == null || refExpr.getQualifier() != null) {
return;
}

GoVarDefinition varDefinition = ObjectUtils.tryCast(refExpr.getReference().resolve(), GoVarDefinition.class);
PsiElement parent = varDefinition != null ? varDefinition.getParent() : null;
if (!(parent instanceof GoRangeClause)) {
return;
}

holder.registerProblem(o, "Suspicious: obtaining address of a for loop variable");
}
};
}
}
7 changes: 7 additions & 0 deletions testData/highlighting/addressOfLoopVariable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func main() {
for _, i := range []int{1, 2, 3} {
println(<weak_warning descr="Suspicious: obtaining address of a for loop variable">&i</weak_warning>)
}
}
5 changes: 4 additions & 1 deletion tests/com/goide/inspections/GoHighlightingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void setUp() throws Exception {
GoPlaceholderCountInspection.class,
GoEmbeddedInterfacePointerInspection.class,
GoStructInitializationInspection.class,
GoMethodOnNonLocalTypeInspection.class
GoMethodOnNonLocalTypeInspection.class,
GoAddressOfLoopVariableInspection.class
);
}

Expand Down Expand Up @@ -350,6 +351,8 @@ public void testUnitializedStructInitialization() {
doWeakTest();
}

public void testAddressOfLoopVariable() { doWeakTest(); }

private long doWeakTest() {return myFixture.testHighlighting(true, false, true, getTestName(true) + ".go");}

public void testDoNotHighlightCommentOfMainPackage() {
Expand Down