IG: ignore casts to concrete class inside equals() and clone() methods (IDEA-148010)

This commit is contained in:
Bas Leijdekkers
2015-11-16 19:09:29 +01:00
parent 0ff65a0815
commit dae504e27f
3 changed files with 16 additions and 16 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 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.
@@ -21,6 +21,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.CloneUtils;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -33,7 +34,7 @@ public class CastToConcreteClassInspection extends BaseInspection {
public boolean ignoreAbstractClasses = false;
@SuppressWarnings("PublicField")
public boolean ignoreInEquals = true;
public boolean ignoreInEquals = true; // keep for compatibility
@Override
@NotNull
@@ -73,11 +74,9 @@ public class CastToConcreteClassInspection extends BaseInspection {
if (!ConcreteClassUtil.typeIsConcreteClass(typeElement, ignoreAbstractClasses)) {
return;
}
if (ignoreInEquals) {
final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class);
if (MethodUtils.isEquals(method)) {
return;
}
final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class);
if (MethodUtils.isEquals(method) || CloneUtils.isClone(method)) {
return;
}
registerError(typeElement, typeElement.getType());
}
@@ -116,11 +115,9 @@ public class CastToConcreteClassInspection extends BaseInspection {
if (!ConcreteClassUtil.typeIsConcreteClass(parameter, ignoreAbstractClasses)) {
return;
}
if (ignoreInEquals) {
final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class);
if (MethodUtils.isEquals(method)) {
return;
}
final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class);
if (MethodUtils.isEquals(method) || CloneUtils.isClone(method)) {
return;
}
registerMethodCallError(expression, parameter);
}
@@ -2,12 +2,10 @@
<body>
Reports casting a value to a concrete class, rather than an interface.
Such declarations may represent a failure of abstraction, and may make testing more difficult.
Declarations whose classes come from system or third-party libraries will not be reported by this inspection.
Casts to system or library classes and casts inside <b>equals()</b> and <b>clone()</b> methods will not be reported by this inspection.
<!-- tooltip end -->
<p>
Use the first checkbox below to have this inspection ignore casts to abstract classes.
<p>
Use the second checkbox below to have this inspection ignore casts inside <code>equals()</code> methods.
Use the checkbox below to have this inspection ignore casts to abstract classes.
<p>
</body>
@@ -14,6 +14,11 @@ class CastToConcreteClass {
}
}
@Override
public CastToConcreteClass clone() throws CloneNotSupportedException {
return (CastToConcreteClass) super.clone();
}
void foo(Object o) {
CastToConcreteClass c = (<warning descr="Cast to concrete class 'CastToConcreteClass'">CastToConcreteClass</warning>)o;
CastToConcreteClass c2 = CastToConcreteClass.class.<warning descr="Cast to concrete class 'CastToConcreteClass'">cast</warning>(o);