redundant cast: always report casts in instanceOf checks for class types; exclude primitive types

This commit is contained in:
anna
2013-08-05 18:16:18 +02:00
parent 20dbc2b67c
commit 5c737f48bd
4 changed files with 23 additions and 2 deletions
@@ -21,7 +21,6 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.source.resolve.DefaultParameterTypeInferencePolicy;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
@@ -494,7 +493,10 @@ public class RedundantCastUtil {
}
}
else {
if (TypeConversionUtil.isAssignable(castTo, opType, false)) {
if (parent instanceof PsiInstanceOfExpression && opType instanceof PsiPrimitiveType) {
return;
}
if (parent instanceof PsiInstanceOfExpression || TypeConversionUtil.isAssignable(castTo, opType, false)) {
addToResults(typeCast);
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Casting &lt;code&gt;o&lt;/code&gt; to &lt;code&gt;List&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
import java.util.ArrayList;
import java.util.List;
public class RedundantCast {
boolean redundantCasts(Object o) {
int p = 0;
if ((Number)p instanceof Integer) {}
return (List)o instanceof ArrayList;
}
}
@@ -47,6 +47,7 @@ public class RedundantCast15Test extends InspectionTestCase {
public void testRawCast1() throws Exception { doTest();}
public void testInferenceFromCast() throws Exception { doTest();}
public void testGetClassProcessing() throws Exception { doTest();}
public void testInstanceOfChecks() throws Exception { doTest();}
public void testTypeParameterAccessChecksJava7() throws Exception {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_7, getModule(), getTestRootDisposable());