Search in sources :

Example 1 with GoDeleteQuickFix

use of com.goide.quickfix.GoDeleteQuickFix 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 2 with GoDeleteQuickFix

use of com.goide.quickfix.GoDeleteQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoDeferGoInspection method buildGoVisitor.

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

        @SuppressWarnings("DialogTitleCapitalization")
        @Override
        public void visitDeferStatement(@NotNull GoDeferStatement o) {
            super.visitDeferStatement(o);
            GoExpression expression = o.getExpression();
            if (expression instanceof GoCallExpr) {
                GoCallExpr callExpr = (GoCallExpr) expression;
                if (GoPsiImplUtil.isPanic(callExpr)) {
                    holder.registerProblem(o, "defer should not call panic() directly #loc", ProblemHighlightType.WEAK_WARNING, new GoDeleteQuickFix("Delete statement", GoDeferStatement.class));
                    return;
                }
                if (GoPsiImplUtil.isRecover(callExpr)) {
                    holder.registerProblem(o, "defer should not call recover() directly #loc", ProblemHighlightType.WEAK_WARNING, new GoReplaceWithCorrectDeferRecoverQuickFix());
                    return;
                }
            }
            checkExpression(expression, "defer");
        }

        @SuppressWarnings("DialogTitleCapitalization")
        @Override
        public void visitGoStatement(@NotNull GoGoStatement o) {
            super.visitGoStatement(o);
            GoExpression expression = o.getExpression();
            if (expression instanceof GoCallExpr) {
                GoCallExpr callExpr = (GoCallExpr) expression;
                if (GoPsiImplUtil.isPanic(callExpr)) {
                    holder.registerProblem(o, "go should not call panic() directly #loc", ProblemHighlightType.WEAK_WARNING, new GoDeleteQuickFix("Delete statement", GoGoStatement.class));
                    return;
                }
                if (GoPsiImplUtil.isRecover(callExpr)) {
                    holder.registerProblem(o, "go should not call recover() directly #loc", ProblemHighlightType.WEAK_WARNING, new GoDeleteQuickFix("Delete statement", GoGoStatement.class));
                    return;
                }
            }
            checkExpression(expression, "go");
        }

        private void checkExpression(@Nullable GoExpression o, @NotNull String who) {
            if (o == null)
                return;
            if (o instanceof GoCallExpr || o instanceof GoBuiltinCallExpr) {
                if (GoPsiImplUtil.isConversionExpression(o)) {
                    holder.registerProblem(o, who + " requires function call, not conversion #loc", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }
                return;
            }
            if (o instanceof GoParenthesesExpr) {
                holder.registerProblem(o, "Expression in " + who + " must not be parenthesized", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new GoUnwrapParensExpression());
            }
            LocalQuickFix[] fixes = o.getGoType(null) instanceof GoFunctionType ? new LocalQuickFix[] { new GoAddParensQuickFix() } : LocalQuickFix.EMPTY_ARRAY;
            holder.registerProblem(o, "Expression in " + who + " must be function call", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fixes);
        }
    };
}
Also used : GoDeleteQuickFix(com.goide.quickfix.GoDeleteQuickFix) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GoDeleteQuickFix (com.goide.quickfix.GoDeleteQuickFix)2 NotNull (org.jetbrains.annotations.NotNull)2 GoFile (com.goide.psi.GoFile)1 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)1 GoVisitor (com.goide.psi.GoVisitor)1 GoRenameToBlankQuickFix (com.goide.quickfix.GoRenameToBlankQuickFix)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1