|
| 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