redundant cast: ignore more specific cast types in foreach statements when opType is enough to be used in foreach expr

This commit is contained in:
anna
2013-08-05 18:16:21 +02:00
parent 9e2d0679db
commit 1bd183256e
4 changed files with 27 additions and 0 deletions
@@ -499,6 +499,12 @@ public class RedundantCastUtil {
if (parent instanceof PsiInstanceOfExpression && opType instanceof PsiPrimitiveType) {
return;
}
if (parent instanceof PsiForeachStatement) {
if (InheritanceUtil.isInheritor(PsiUtil.resolveClassInType(opType), false, CommonClassNames.JAVA_LANG_ITERABLE)) {
addToResults(typeCast);
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>7</line>
<description>Casting &lt;code&gt;list&lt;/code&gt; to &lt;code&gt;ArrayList&lt;String&gt;&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
import java.util.ArrayList;
import java.util.List;
public class RedundantCast {
void redundantCasts() {
List<String> list = new ArrayList<>();
for (String s : (ArrayList<String>) list) {}
Object o = new ArrayList<>();
for (String s : (ArrayList<String>) o) {}
}
}
@@ -48,6 +48,7 @@ public class RedundantCast15Test extends InspectionTestCase {
public void testInferenceFromCast() throws Exception { doTest();}
public void testGetClassProcessing() throws Exception { doTest();}
public void testInstanceOfChecks() throws Exception { doTest();}
public void testForEachValue() throws Exception { doTest();}
public void testTypeParameterAccessChecksJava7() throws Exception {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_7, getModule(), getTestRootDisposable());