Search in sources :

Example 1 with GoVisitor

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

the class GoAssignmentToReceiverInspection method buildGoVisitor.

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

        @Override
        public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
            super.visitReferenceExpression(o);
            if (o.getReadWriteAccess() == ReadWriteAccessDetector.Access.Write) {
                PsiElement resolve = o.resolve();
                if (resolve instanceof GoReceiver) {
                    String message = "Assignment to method receiver doesn't propagate to other calls";
                    if (((GoReceiver) resolve).getType() instanceof GoPointerType) {
                        if (o.getParent() instanceof GoUnaryExpr) {
                            GoUnaryExpr p = (GoUnaryExpr) o.getParent();
                            if (p.getMul() != null) {
                                // pointer dereference
                                return;
                            }
                        }
                        message = "Assignment to method receiver propagates only to callees but not to callers";
                    }
                    holder.registerProblem(o, message, WEAK_WARNING);
                }
            }
        }
    };
}
Also used : GoUnaryExpr(com.goide.psi.GoUnaryExpr) GoPointerType(com.goide.psi.GoPointerType) GoReceiver(com.goide.psi.GoReceiver) GoReferenceExpression(com.goide.psi.GoReferenceExpression) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GoVisitor

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

the class GoUnusedFunctionInspection method buildGoVisitor.

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

        @Override
        public void visitFunctionDeclaration(@NotNull GoFunctionDeclaration o) {
            if (o.isBlank())
                return;
            GoFile file = o.getContainingFile();
            String name = o.getName();
            if (!canRun(name))
                return;
            if (GoConstants.MAIN.equals(file.getPackageName()) && GoConstants.MAIN.equals(name))
                return;
            if (GoConstants.INIT.equals(name))
                return;
            if (GoTestFinder.isTestFile(file) && GoTestFunctionType.fromName(name) != null)
                return;
            if (ReferencesSearch.search(o, o.getUseScope()).findFirst() == null) {
                PsiElement id = o.getIdentifier();
                TextRange range = TextRange.from(id.getStartOffsetInParent(), id.getTextLength());
                holder.registerProblem(o, "Unused function <code>#ref</code> #loc", ProblemHighlightType.LIKE_UNUSED_SYMBOL, range, new GoDeleteQuickFix("Delete function", GoFunctionDeclaration.class), new GoRenameToBlankQuickFix(o));
            }
        }
    };
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) GoFile(com.goide.psi.GoFile) GoRenameToBlankQuickFix(com.goide.quickfix.GoRenameToBlankQuickFix) TextRange(com.intellij.openapi.util.TextRange) GoDeleteQuickFix(com.goide.quickfix.GoDeleteQuickFix) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GoVisitor

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

the class GoUsedAsValueInCondition method buildGoVisitor.

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

        @Override
        public void visitAssignmentStatement(@NotNull GoAssignmentStatement o) {
            if (o.getParent() != null && o.getParent() instanceof GoIfStatement && ((GoIfStatement) o.getParent()).getExpression() == null) {
                String left = GoPsiImplUtil.joinPsiElementText(o.getLeftHandExprList().getExpressionList());
                String right = GoPsiImplUtil.joinPsiElementText(o.getExpressionList());
                holder.registerProblem(o, left + " = " + right + " used as value", GENERIC_ERROR_OR_WARNING, new GoAssignmentToComparisonQuickFix());
            }
        }

        @Override
        public void visitShortVarDeclaration(@NotNull GoShortVarDeclaration o) {
            PsiElement parent = o.getParent();
            if (parent != null) {
                PsiElement gradParent = parent.getParent();
                if (gradParent instanceof GoIfStatement && ((GoIfStatement) gradParent).getExpression() == null) {
                    String left = GoPsiImplUtil.joinPsiElementText(o.getVarDefinitionList());
                    String right = GoPsiImplUtil.joinPsiElementText(o.getRightExpressionsList());
                    holder.registerProblem(o, left + " := " + right + " used as value", GENERIC_ERROR_OR_WARNING, new GoAssignmentToComparisonQuickFix());
                }
            }
        }
    };
}
Also used : GoShortVarDeclaration(com.goide.psi.GoShortVarDeclaration) GoAssignmentStatement(com.goide.psi.GoAssignmentStatement) GoIfStatement(com.goide.psi.GoIfStatement) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GoVisitor

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

the class GoReceiverNamesInspection method buildGoVisitor.

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

        @Override
        public void visitReceiver(@NotNull GoReceiver o) {
            if (genericNamesSet.contains(o.getName())) {
                PsiElement identifier = o.getIdentifier();
                if (identifier == null)
                    return;
                holder.registerProblem(identifier, "Receiver has generic name", new GoRenameQuickFix(o));
            }
        }
    };
}
Also used : GoRenameQuickFix(com.goide.quickfix.GoRenameQuickFix) GoReceiver(com.goide.psi.GoReceiver) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GoVisitor

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

the class GoUnusedConstInspection method buildGoVisitor.

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

        @Override
        public void visitConstDefinition(@NotNull GoConstDefinition o) {
            if (o.isBlank())
                return;
            if (ReferencesSearch.search(o, o.getUseScope()).findFirst() == null) {
                String constName = o.getName();
                holder.registerProblem(o, "Unused constant <code>#ref</code> #loc", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new GoDeleteConstDefinitionQuickFix(constName));
            }
        }
    };
}
Also used : GoDeleteConstDefinitionQuickFix(com.goide.quickfix.GoDeleteConstDefinitionQuickFix) GoConstDefinition(com.goide.psi.GoConstDefinition) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GoVisitor (com.goide.psi.GoVisitor)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiElement (com.intellij.psi.PsiElement)4 GoReceiver (com.goide.psi.GoReceiver)2 GoRenameToBlankQuickFix (com.goide.quickfix.GoRenameToBlankQuickFix)2 GoAssignmentStatement (com.goide.psi.GoAssignmentStatement)1 GoConstDefinition (com.goide.psi.GoConstDefinition)1 GoExpression (com.goide.psi.GoExpression)1 GoFile (com.goide.psi.GoFile)1 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)1 GoIfStatement (com.goide.psi.GoIfStatement)1 GoLabelDefinition (com.goide.psi.GoLabelDefinition)1 GoPointerType (com.goide.psi.GoPointerType)1 GoRangeClause (com.goide.psi.GoRangeClause)1 GoReferenceExpression (com.goide.psi.GoReferenceExpression)1 GoShortVarDeclaration (com.goide.psi.GoShortVarDeclaration)1 GoType (com.goide.psi.GoType)1 GoUnaryExpr (com.goide.psi.GoUnaryExpr)1 GoDeleteConstDefinitionQuickFix (com.goide.quickfix.GoDeleteConstDefinitionQuickFix)1 GoDeleteQuickFix (com.goide.quickfix.GoDeleteQuickFix)1