-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakub Zawadzki
committed
Feb 19, 2017
1 parent
eb5fc24
commit 54d3087
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
95
src/com/goide/inspections/GoStringCannotBeNilInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |