Search in sources :

Example 1 with GoReferenceExpressionImpl

use of com.goide.psi.impl.GoReferenceExpressionImpl in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoAssignmentNilWithoutExplicitTypeInspection method buildGoVisitor.

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

        @Override
        public void visitVarDeclaration(@NotNull GoVarDeclaration o) {
            for (GoVarSpec spec : o.getVarSpecList()) {
                checkVar(spec);
            }
        }

        @Override
        public void visitShortVarDeclaration(@NotNull GoShortVarDeclaration o) {
            checkVar(o);
        }

        @Override
        public void visitConstDeclaration(@NotNull GoConstDeclaration o) {
            for (GoConstSpec spec : o.getConstSpecList()) {
                checkConst(spec);
            }
        }

        private void checkVar(@NotNull GoVarSpec spec) {
            if (spec.getType() != null)
                return;
            checkExpressions(spec.getRightExpressionsList());
        }

        private void checkConst(@NotNull GoConstSpec spec) {
            if (spec.getType() != null)
                return;
            checkExpressions(spec.getExpressionList());
        }

        private void checkExpressions(@NotNull List<GoExpression> expressions) {
            for (GoExpression expr : expressions) {
                if (expr instanceof GoReferenceExpressionImpl) {
                    GoReferenceExpressionImpl ref = (GoReferenceExpressionImpl) expr;
                    PsiElement resolve = ref.resolve();
                    if (ref.getIdentifier().textMatches(GoConstants.NIL) && resolve != null && GoPsiImplUtil.builtin(resolve)) {
                        holder.registerProblem(expr, "Cannot assign nil without explicit type", GENERIC_ERROR_OR_WARNING);
                    }
                }
            }
        }
    };
}
Also used : GoReferenceExpressionImpl(com.goide.psi.impl.GoReferenceExpressionImpl) List(java.util.List) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GoReferenceExpressionImpl (com.goide.psi.impl.GoReferenceExpressionImpl)1 PsiElement (com.intellij.psi.PsiElement)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1