Skip to content

Commit

Permalink
Cleanup after merge
Browse files Browse the repository at this point in the history
- extract 'nil' constant
- use proper visitor method
- replace getText().equals with textMatches
  • Loading branch information
zolotov committed Jul 28, 2015
1 parent 26c94b2 commit 460ee4d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
29 changes: 9 additions & 20 deletions src/com/goide/inspections/GoAssignmentNilWithoutExplicitType.java
Expand Up @@ -16,38 +16,27 @@

package com.goide.inspections;

import com.goide.GoConstants;
import com.goide.psi.*;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import static com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING;

public class GoAssignmentNilWithoutExplicitType extends GoInspectionBase {
@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull final ProblemsHolder holder,
@SuppressWarnings({"UnusedParameters", "For future"}) @NotNull LocalInspectionToolSession session) {
protected GoVisitor buildGoVisitor(@NotNull final ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
return new GoVisitor() {
@Override
public void visitStatement(@NotNull GoStatement o) {
GoVarDeclaration declaration = o.getVarDeclaration();
if (declaration != null) {
List<GoVarSpec> specs = declaration.getVarSpecList();
for (GoVarSpec spec : specs) {

if (spec.getType() != null)
continue;

List<GoExpression> expressions = spec.getExpressionList();
for (GoExpression expr : expressions) {
if (expr instanceof GoReferenceExpression) {
String name = ((GoReferenceExpression)expr).getIdentifier().getText();
if (name != null && name.equals("nil")) {
holder.registerProblem(expr, "Cannot assign nil without explicit type", GENERIC_ERROR_OR_WARNING);
}
public void visitVarDeclaration(@NotNull GoVarDeclaration o) {
for (GoVarSpec spec : o.getVarSpecList()) {
if (spec.getType() != null) continue;
for (GoExpression expr : spec.getExpressionList()) {
if (expr instanceof GoReferenceExpression) {
if (((GoReferenceExpression)expr).getIdentifier().textMatches(GoConstants.NIL)) {
holder.registerProblem(expr, "Cannot assign nil without explicit type", GENERIC_ERROR_OR_WARNING);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/com/goide/psi/impl/GoTypeReference.java
Expand Up @@ -16,6 +16,7 @@

package com.goide.psi.impl;

import com.goide.GoConstants;
import com.goide.GoTypes;
import com.goide.psi.*;
import com.goide.sdk.GoSdkUtil;
Expand Down Expand Up @@ -128,15 +129,15 @@ private boolean processUnqualifiedResolve(@NotNull GoFile file,
if (!GoReference.processDirectory(dir, file, file.getPackageName(), processor, state, true)) return false;
if (GoReference.processImports(file, processor, state, myElement)) return false;
if (processBuiltin(processor, state, myElement)) return false;
if (getIdentifier().textMatches("nil") && PsiTreeUtil.getParentOfType(myElement, GoTypeCaseClause.class) != null) {
if (getIdentifier().textMatches(GoConstants.NIL) && PsiTreeUtil.getParentOfType(myElement, GoTypeCaseClause.class) != null) {
GoType type = PsiTreeUtil.getParentOfType(myElement, GoType.class);
if (FormatterUtil.getPrevious(type != null ? type.getNode() : null, GoTypes.CASE) == null) return true;
GoFile builtinFile = GoSdkUtil.findBuiltinFile(myElement);
if (builtinFile == null) return false;
GoVarDefinition nil = ContainerUtil.find(builtinFile.getVars(), new Condition<GoVarDefinition>() {
@Override
public boolean value(GoVarDefinition v) {
return "nil".equals(v.getName());
return GoConstants.NIL.equals(v.getName());
}
});
if (nil != null && !processor.execute(nil, state)) return false;
Expand Down
1 change: 1 addition & 0 deletions utils/src/com/goide/GoConstants.java
Expand Up @@ -85,6 +85,7 @@ public class GoConstants {
"windows/amd64", "linux/ppc64le");
public static final Set<String> KNOWN_COMPILERS = ContainerUtil.immutableSet("gc", "gccgo");

@NonNls public static final String NIL = "nil";
private GoConstants() {

}
Expand Down

0 comments on commit 460ee4d

Please sign in to comment.