diff --git a/plugins/devkit/resources/META-INF/plugin.xml b/plugins/devkit/resources/META-INF/plugin.xml index c41da139094a..fadad369c4b0 100644 --- a/plugins/devkit/resources/META-INF/plugin.xml +++ b/plugins/devkit/resources/META-INF/plugin.xml @@ -143,6 +143,11 @@ groupKey="inspections.group.name" enabledByDefault="true" level="WARNING" implementationClass="org.jetbrains.idea.devkit.inspections.internal.DontUseNewPairInspection"/> + diff --git a/plugins/devkit/resources/inspectionDescriptions/UseVirtualFileEquals.html b/plugins/devkit/resources/inspectionDescriptions/UseVirtualFileEquals.html new file mode 100644 index 000000000000..83e99b3d0433 --- /dev/null +++ b/plugins/devkit/resources/inspectionDescriptions/UseVirtualFileEquals.html @@ -0,0 +1,5 @@ + + +Use equals instead of == when comparing VirtualFile objects + + \ No newline at end of file diff --git a/plugins/devkit/src/inspections/internal/UseVirtualFileEqualsInspection.java b/plugins/devkit/src/inspections/internal/UseVirtualFileEqualsInspection.java new file mode 100644 index 000000000000..e713fad347af --- /dev/null +++ b/plugins/devkit/src/inspections/internal/UseVirtualFileEqualsInspection.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.idea.devkit.inspections.internal; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.*; +import com.intellij.psi.util.InheritanceUtil; +import com.siyeh.ig.psiutils.ComparisonUtils; +import org.jetbrains.annotations.NotNull; + +/** + * @author peter + */ +public class UseVirtualFileEqualsInspection extends InternalInspection { + @Override + public PsiElementVisitor buildInternalVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { + return new JavaElementVisitor() { + @Override + public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { + super.visitBinaryExpression(expression); + if (!ComparisonUtils.isEqualityComparison(expression)) { + return; + } + final PsiExpression lhs = expression.getLOperand(); + final PsiExpression rhs = expression.getROperand(); + if (rhs == null || lhs.textMatches(PsiKeyword.NULL) || rhs.textMatches(PsiKeyword.NULL)) { + return; + } + + if (InheritanceUtil.isInheritor(lhs.getType(), VirtualFile.class.getName()) || InheritanceUtil.isInheritor(rhs.getType(), VirtualFile.class.getName())) { + holder.registerProblem(expression, "VirtualFile objects should be compared by equals(), not ==", ProblemHighlightType.GENERIC_ERROR_OR_WARNING); + + } + } + }; + } +}