From 1bd183256e4a9d23bdcd1bf4085203a48c96fe9a Mon Sep 17 00:00:00 2001 From: anna Date: Mon, 5 Aug 2013 14:48:14 +0200 Subject: [PATCH] redundant cast: ignore more specific cast types in foreach statements when opType is enough to be used in foreach expr --- .../src/com/intellij/psi/util/RedundantCastUtil.java | 6 ++++++ .../redundantCast/generics/ForEachValue/expected.xml | 8 ++++++++ .../generics/ForEachValue/src/Test.java | 12 ++++++++++++ .../intellij/codeInspection/RedundantCast15Test.java | 1 + 4 files changed, 27 insertions(+) create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/src/Test.java diff --git a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java index eb4051375899..551e6e9cb1ff 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java @@ -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); } diff --git a/java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/expected.xml new file mode 100644 index 000000000000..70d6f7378232 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/expected.xml @@ -0,0 +1,8 @@ + + + + Test.java + 7 + Casting <code>list</code> to <code>ArrayList<String></code> is redundant + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/src/Test.java new file mode 100644 index 000000000000..db0b7d198549 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/ForEachValue/src/Test.java @@ -0,0 +1,12 @@ +import java.util.ArrayList; +import java.util.List; + +public class RedundantCast { + void redundantCasts() { + List list = new ArrayList<>(); + for (String s : (ArrayList) list) {} + + Object o = new ArrayList<>(); + for (String s : (ArrayList) o) {} + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java index 936a0ed777d2..4581cf84f3f6 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java @@ -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());