Skip to content

Commit

Permalink
Strings cannot be nil inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Zawadzki committed Feb 19, 2017
1 parent eb5fc24 commit 54d3087
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions resources/META-INF/gogland.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@
<localInspection language="go" displayName="Multiple packages in directory declaration" groupPath="Go"
groupName="General" enabledByDefault="true" level="ERROR"
implementationClass="com.goide.inspections.GoMultiplePackagesInspection"/>
<localInspection language="go" displayName="String cannot be nil" groupPath="Go"
groupName="General" enabledByDefault="true" level="ERROR"
implementationClass="com.goide.inspections.GoStringCannotBeNilInspection"/>
<localInspection language="go" displayName="Usage of cgo in tests is not supported" groupPath="Go"
groupName="General" enabledByDefault="true" level="ERROR"
implementationClass="com.goide.inspections.GoCgoInTestInspection"/>
Expand Down
21 changes: 21 additions & 0 deletions resources/inspectionDescriptions/GoStringCannotBeNil.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2013-2017 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>
Inspects for string assignation or comparision to nil.
</body>
</html>
95 changes: 95 additions & 0 deletions src/com/goide/inspections/GoStringCannotBeNilInspection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.goide.inspections;

import com.goide.GoConstants;
import com.goide.GoTypes;
import com.goide.psi.*;
import com.goide.psi.impl.GoElementFactory;
import com.goide.psi.impl.GoTypeUtil;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GoStringCannotBeNilInspection extends GoInspectionBase{

private static final String DEFAULT_STRING = "\"\"";
private static final String PROBLEM_DESC = "String cannot be nil";

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
return new GoVisitor(){


@Override
public void visitVarSpec(@NotNull GoVarSpec o) {
super.visitVarSpec(o);
for(int i = 0; i < o.getExpressionList().size(); ++i){
check(o.getVarDefinitionList().get(i).getGoType(null), o.getVarDefinitionList().get(i).getValue());
}
}


@Override
public void visitAssignOp(@NotNull GoAssignOp o) {
super.visitAssignOp(o);
PsiElement[] right = o.getParent().getChildren();
if(right == null) return;

List<GoExpression> left = ((GoLeftHandExprList)right[0]).getExpressionList();
if(left == null) return;

for(int i = 0; i < left.size(); ++i){
GoExpression var = left.get(i);
PsiElement value = right[right.length - (left.size() - i)];

if(value instanceof GoExpression){
check(var.getGoType(null), (GoExpression)value);
}

}
}

@Override
public void visitBinaryExpr(@NotNull GoBinaryExpr o) {
super.visitBinaryExpr(o);

if(o.getOperator().getText().equals(GoTypes.EQ.toString()) ||
o.getOperator().getText().equals(GoTypes.NOT_EQ.toString())){
check(o.getLeft().getGoType(null), o.getRight());
check(o.getRight().getGoType(null), o.getLeft());
}
}

protected void check(GoType var, GoExpression value){

if(var == null || value == null) return;

if(GoTypeUtil.isString(var)) {
if(value.getText().equals(GoConstants.NIL)){

holder.registerProblem(value, PROBLEM_DESC, new LocalQuickFix() {
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Change to default value";
}

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
value.replace(GoElementFactory.createExpression(project, DEFAULT_STRING));
}
});
}
}
}
};
}
}

0 comments on commit 54d3087

Please sign in to comment.