Skip to content

Commit 0632a7c

Browse files
stuartcarnieignatov
authored andcommitted
no warning for assignment to dereferenced receiver (#2809)
1 parent 07a6fc1 commit 0632a7c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/com/goide/inspections/GoAssignmentToReceiverInspection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.goide.psi.GoPointerType;
2020
import com.goide.psi.GoReceiver;
2121
import com.goide.psi.GoReferenceExpression;
22+
import com.goide.psi.GoUnaryExpr;
2223
import com.goide.psi.GoVisitor;
2324
import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector;
2425
import com.intellij.codeInspection.LocalInspectionToolSession;
@@ -41,6 +42,13 @@ public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
4142
if (resolve instanceof GoReceiver) {
4243
String message = "Assignment to method receiver doesn't propagate to other calls";
4344
if (((GoReceiver)resolve).getType() instanceof GoPointerType) {
45+
if (o.getParent() instanceof GoUnaryExpr) {
46+
GoUnaryExpr p = (GoUnaryExpr)o.getParent();
47+
if (p.getMul() != null) {
48+
// pointer dereference
49+
return;
50+
}
51+
}
4452
message = "Assignment to method receiver propagates only to callees but not to callers";
4553
}
4654
holder.registerProblem(o, message, WEAK_WARNING);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.GoCodeInsightFixtureTestCase;
20+
21+
public class GoAssignmentToReceiverInspectionTest extends GoCodeInsightFixtureTestCase {
22+
23+
@Override
24+
public void setUp() throws Exception {
25+
super.setUp();
26+
myFixture.enableInspections(GoAssignmentToReceiverInspection.class);
27+
}
28+
29+
public void testDoesNotPropagateToOtherCallsForNonPointerReceiver() {
30+
doTest("func (r MyType) foo() { <weak_warning descr=\"Assignment to method receiver doesn't propagate to other calls\">r<caret></weak_warning> = 0 }");
31+
}
32+
33+
public void testDoesNotPropagateToOtherCallsForPointerReceiver() {
34+
doTest("func (r *MyType) foo() { <weak_warning descr=\"Assignment to method receiver propagates only to callees but not to callers\">r<caret></weak_warning> = 0 }");
35+
}
36+
37+
public void testNoFixForDereferencedPointerReceiver() {
38+
doTest("func (r *MyType) foo() { *r<caret> = 0 }");
39+
}
40+
41+
private void doTest(String code) {
42+
myFixture.configureByText("a.go", "package main; type MyType int; " + code);
43+
myFixture.checkHighlighting();
44+
}
45+
}

0 commit comments

Comments
 (0)