Search in sources :

Example 1 with GoReferenceExpression

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

the class GoSimplifyBoolExprQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!(startElement instanceof GoBinaryExpr))
        return;
    GoBinaryExpr o = (GoBinaryExpr) startElement;
    boolean and = o instanceof GoAndExpr;
    List<GoExpression> elements = GoBoolExpressionsInspection.collect(o, and);
    List<GoExpression> toRemove = ContainerUtil.newSmartList();
    for (int i = 0; i < elements.size(); i++) {
        GoExpression l = elements.get(i);
        if (l instanceof GoReferenceExpression && (l.textMatches("true") || l.textMatches("false")) && GoPsiImplUtil.builtin(((GoReferenceExpression) l).resolve())) {
            boolean trueExpr = l.textMatches("true");
            if (and ^ !trueExpr) {
                toRemove.add(l);
            } else {
                replaceExpressionByBoolConst(o, project, !and);
                return;
            }
        }
        for (int j = i + 1; j < elements.size(); j++) {
            GoExpression r = elements.get(j);
            if (GoBoolExpressionsInspection.isEqualsWithNot(l, r) || GoBoolExpressionsInspection.isEqualsWithNot(r, l)) {
                replaceExpressionByBoolConst(o, project, !and);
            }
            if (GoExpressionUtil.identical(l, r))
                toRemove.add(l);
        // todo expr evaluating! x != c1 || x != c2 (c1, c2 const, c1 != c2)
        }
    }
    if (!toRemove.isEmpty()) {
        removeRedundantExpressions(o, project, elements, toRemove, and);
    }
}
Also used : GoBinaryExpr(com.goide.psi.GoBinaryExpr) GoReferenceExpression(com.goide.psi.GoReferenceExpression) GoExpression(com.goide.psi.GoExpression) GoAndExpr(com.goide.psi.GoAndExpr)

Example 2 with GoReferenceExpression

use of com.goide.psi.GoReferenceExpression 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)

Aggregations

GoReferenceExpression (com.goide.psi.GoReferenceExpression)2 GoAndExpr (com.goide.psi.GoAndExpr)1 GoBinaryExpr (com.goide.psi.GoBinaryExpr)1 GoExpression (com.goide.psi.GoExpression)1 GoPointerType (com.goide.psi.GoPointerType)1 GoReceiver (com.goide.psi.GoReceiver)1 GoUnaryExpr (com.goide.psi.GoUnaryExpr)1 GoVisitor (com.goide.psi.GoVisitor)1 PsiElement (com.intellij.psi.PsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1