IG: don't warn on interfaces (IDEA-163830)

This commit is contained in:
Bas Leijdekkers
2017-01-11 19:21:41 +01:00
parent 806675a980
commit 9b2741f674
4 changed files with 78 additions and 8 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2010 Bas Leijdekkers
* Copyright 2006-2017 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
package com.siyeh.ig.bugs;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.MethodSignatureUtil;
import com.siyeh.HardcodedMethodConstants;
@@ -26,8 +29,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
public class ComparableImplementedButEqualsNotOverriddenInspection
extends BaseInspection {
public class ComparableImplementedButEqualsNotOverriddenInspection extends BaseInspection {
@Override
@NotNull
@@ -48,14 +50,15 @@ public class ComparableImplementedButEqualsNotOverriddenInspection
return new CompareToAndEqualsNotPairedVisitor();
}
private static class CompareToAndEqualsNotPairedVisitor
extends BaseInspectionVisitor {
private static class CompareToAndEqualsNotPairedVisitor extends BaseInspectionVisitor {
@Override
public void visitClass(PsiClass aClass) {
super.visitClass(aClass);
final PsiMethod[] methods = aClass.findMethodsByName(
HardcodedMethodConstants.COMPARE_TO, false);
if (aClass.isInterface()) {
return;
}
final PsiMethod[] methods = aClass.findMethodsByName(HardcodedMethodConstants.COMPARE_TO, false);
if (methods.length == 0) {
return;
}
@@ -0,0 +1,25 @@
interface ComparableInterface extends Comparable<ComparableInterface> {
int get();
}
interface ComparableImpl extends ComparableInterface {
@Override
default int compareTo(ComparableInterface o) {
return 0;
}
}
abstract class Impl implements ComparableImpl {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Impl impl = (Impl) o;
return get() == impl.get();
}
@Override
public int hashCode() {
return get();
}
}
@@ -0,0 +1,7 @@
class <warning descr="Class 'Simple' implements 'java.lang.Comparable' but does not override 'equals()'">Simple</warning> implements Comparable<Simple> {
public int compareTo(Simple other) {
return 0;
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2000-2017 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 com.siyeh.ig.bugs;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class ComparableImplementedButEqualsNotOverriddenInspectionTest extends LightInspectionTestCase {
public void testInterfaceImplementingComparable() { doTest(); }
public void testSimple() { doTest(); }
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new ComparableImplementedButEqualsNotOverriddenInspection();
}
}