Skip to content

Commit 54d3087

Browse files
author
Jakub Zawadzki
committed
Strings cannot be nil inspection
1 parent eb5fc24 commit 54d3087

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

resources/META-INF/gogland.xml

+3
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@
329329
<localInspection language="go" displayName="Multiple packages in directory declaration" groupPath="Go"
330330
groupName="General" enabledByDefault="true" level="ERROR"
331331
implementationClass="com.goide.inspections.GoMultiplePackagesInspection"/>
332+
<localInspection language="go" displayName="String cannot be nil" groupPath="Go"
333+
groupName="General" enabledByDefault="true" level="ERROR"
334+
implementationClass="com.goide.inspections.GoStringCannotBeNilInspection"/>
332335
<localInspection language="go" displayName="Usage of cgo in tests is not supported" groupPath="Go"
333336
groupName="General" enabledByDefault="true" level="ERROR"
334337
implementationClass="com.goide.inspections.GoCgoInTestInspection"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
~ Copyright 2013-2017 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+
Inspects for string assignation or comparision to nil.
20+
</body>
21+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.goide.inspections;
2+
3+
import com.goide.GoConstants;
4+
import com.goide.GoTypes;
5+
import com.goide.psi.*;
6+
import com.goide.psi.impl.GoElementFactory;
7+
import com.goide.psi.impl.GoTypeUtil;
8+
import com.intellij.codeInspection.LocalInspectionToolSession;
9+
import com.intellij.codeInspection.LocalQuickFix;
10+
import com.intellij.codeInspection.ProblemDescriptor;
11+
import com.intellij.codeInspection.ProblemsHolder;
12+
import com.intellij.openapi.project.Project;
13+
import com.intellij.psi.PsiElement;
14+
import org.jetbrains.annotations.Nls;
15+
import org.jetbrains.annotations.NotNull;
16+
17+
import java.util.List;
18+
19+
public class GoStringCannotBeNilInspection extends GoInspectionBase{
20+
21+
private static final String DEFAULT_STRING = "\"\"";
22+
private static final String PROBLEM_DESC = "String cannot be nil";
23+
24+
@NotNull
25+
@Override
26+
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
27+
return new GoVisitor(){
28+
29+
30+
@Override
31+
public void visitVarSpec(@NotNull GoVarSpec o) {
32+
super.visitVarSpec(o);
33+
for(int i = 0; i < o.getExpressionList().size(); ++i){
34+
check(o.getVarDefinitionList().get(i).getGoType(null), o.getVarDefinitionList().get(i).getValue());
35+
}
36+
}
37+
38+
39+
@Override
40+
public void visitAssignOp(@NotNull GoAssignOp o) {
41+
super.visitAssignOp(o);
42+
PsiElement[] right = o.getParent().getChildren();
43+
if(right == null) return;
44+
45+
List<GoExpression> left = ((GoLeftHandExprList)right[0]).getExpressionList();
46+
if(left == null) return;
47+
48+
for(int i = 0; i < left.size(); ++i){
49+
GoExpression var = left.get(i);
50+
PsiElement value = right[right.length - (left.size() - i)];
51+
52+
if(value instanceof GoExpression){
53+
check(var.getGoType(null), (GoExpression)value);
54+
}
55+
56+
}
57+
}
58+
59+
@Override
60+
public void visitBinaryExpr(@NotNull GoBinaryExpr o) {
61+
super.visitBinaryExpr(o);
62+
63+
if(o.getOperator().getText().equals(GoTypes.EQ.toString()) ||
64+
o.getOperator().getText().equals(GoTypes.NOT_EQ.toString())){
65+
check(o.getLeft().getGoType(null), o.getRight());
66+
check(o.getRight().getGoType(null), o.getLeft());
67+
}
68+
}
69+
70+
protected void check(GoType var, GoExpression value){
71+
72+
if(var == null || value == null) return;
73+
74+
if(GoTypeUtil.isString(var)) {
75+
if(value.getText().equals(GoConstants.NIL)){
76+
77+
holder.registerProblem(value, PROBLEM_DESC, new LocalQuickFix() {
78+
@Nls
79+
@NotNull
80+
@Override
81+
public String getFamilyName() {
82+
return "Change to default value";
83+
}
84+
85+
@Override
86+
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
87+
value.replace(GoElementFactory.createExpression(project, DEFAULT_STRING));
88+
}
89+
});
90+
}
91+
}
92+
}
93+
};
94+
}
95+
}

0 commit comments

Comments
 (0)