mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
@@ -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 <code>list</code> to <code>ArrayList<String></code> 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());
|
||||
|
||||
Reference in New Issue
Block a user