IG: don't warn on covariant equals() when there is a super method

This commit is contained in:
Bas Leijdekkers
2016-07-04 21:01:36 +02:00
parent 02d553dde1
commit 2d97a336b9
2 changed files with 35 additions and 16 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2016 Dave Griffith, 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,10 +16,12 @@
package com.siyeh.ig.bugs;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.SuperMethodsSearch;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.MethodUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
@@ -71,25 +73,14 @@ public class CovariantEqualsInspection extends BaseInspection {
}
final PsiMethod[] methods = aClass.getMethods();
for (PsiMethod method1 : methods) {
if (isNonVariantEquals(method1)) {
if (MethodUtils.isEquals(method1)) {
return;
}
}
if (SuperMethodsSearch.search(method, null, true, false).findFirst() != null) {
return;
}
registerMethodError(method);
}
private static boolean isNonVariantEquals(PsiMethod method) {
final String name = method.getName();
if (!HardcodedMethodConstants.EQUALS.equals(name)) {
return false;
}
final PsiParameterList paramList = method.getParameterList();
final PsiParameter[] parameters = paramList.getParameters();
if (parameters.length != 1) {
return false;
}
final PsiType argType = parameters[0].getType();
return TypeUtils.isJavaLangObject(argType);
}
}
}
@@ -1,3 +1,31 @@
interface I {
boolean equals(I i);
}
class A {
public boolean <warning descr="'equals()' should take 'Object' as its argument">equals</warning>(A a) {
return false;
}
}
class B extends A {
@Override
public boolean equals(A a) {
return super.equals(a);
}
}
class C {
public boolean equals(C c) {
return false;
}
public boolean equals(Object o) {
return true;
}
}
class D implements I {
@Override
public boolean equals(I i) {
return false;
}
}