|
| 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.GoPsiImplUtil; |
| 8 | +import com.goide.psi.impl.GoTypeUtil; |
| 9 | +import com.intellij.codeInspection.*; |
| 10 | +import com.intellij.openapi.project.Project; |
| 11 | +import com.intellij.psi.PsiElement; |
| 12 | +import com.intellij.psi.PsiReference; |
| 13 | +import com.intellij.psi.tree.IElementType; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.IntStream; |
| 18 | + |
| 19 | +public class GoStringCannotBeNilInspection extends GoInspectionBase{ |
| 20 | + public static final String QUICK_FIX_NAME = "Change to default value"; |
| 21 | + public static final String DEFAULT_STRING = "\"\""; |
| 22 | + public static final String PROBLEM_DESCRIPTION = "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 | + @Override |
| 30 | + public void visitVarSpec(@NotNull GoVarSpec o) { |
| 31 | + super.visitVarSpec(o); |
| 32 | + |
| 33 | + if(o.getVarDefinitionList() == null) return; |
| 34 | + o.getVarDefinitionList().forEach(var -> check(var, var != null ? var.getValue() : null)); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void visitAssignmentStatement(@NotNull GoAssignmentStatement o) { |
| 39 | + super.visitAssignmentStatement(o); |
| 40 | + |
| 41 | + if(o.getLeftHandExprList() == null) return; |
| 42 | + List<GoExpression> rightSide = o.getExpressionList(); |
| 43 | + List<GoExpression> leftSide = o.getLeftHandExprList().getExpressionList(); |
| 44 | + if(leftSide == null || rightSide == null) return; |
| 45 | + |
| 46 | + IntStream.range(0 , Math.min(leftSide.size(), rightSide.size())) |
| 47 | + .forEach(i -> check(leftSide.get(i), rightSide.get(i))); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void visitBinaryExpr(@NotNull GoBinaryExpr o) { |
| 52 | + super.visitBinaryExpr(o); |
| 53 | + if(o.getOperator() == null || o.getOperator().getNode() == null) return; |
| 54 | + IElementType type = o.getOperator().getNode().getElementType(); |
| 55 | + if(type == GoTypes.EQ || type == GoTypes.NOT_EQ){ |
| 56 | + check(o.getLeft(), o.getRight()); |
| 57 | + check(o.getRight(), o.getLeft()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected void check(GoTypeOwner var, GoExpression value){ |
| 62 | + |
| 63 | + if(var == null || value == null) return; |
| 64 | + GoType varType = var.getGoType(null); |
| 65 | + if(!GoTypeUtil.isString(varType)) return; |
| 66 | + PsiReference valueRef = value.getReference(); |
| 67 | + if(valueRef == null) return; |
| 68 | + PsiElement valueResolved = valueRef.resolve(); |
| 69 | + if(value.textMatches(GoConstants.NIL) && valueResolved != null && GoPsiImplUtil.builtin(valueResolved)){ |
| 70 | + holder.registerProblem(value, PROBLEM_DESCRIPTION, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 71 | + new GoChangeStringToDefaultValueQuickFix()); |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + public static class GoChangeStringToDefaultValueQuickFix extends LocalQuickFixBase { |
| 78 | + |
| 79 | + public GoChangeStringToDefaultValueQuickFix() { |
| 80 | + super(QUICK_FIX_NAME); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { |
| 85 | + PsiElement element = descriptor.getPsiElement(); |
| 86 | + if(element == null) return; |
| 87 | + element.replace(GoElementFactory.createExpression(project, DEFAULT_STRING)); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments