diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java index b1cd9c1eaacf..3675b7cd445a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java @@ -809,6 +809,11 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase { private static String getVariableReferenceText(PsiReferenceExpression reference, PsiVariable variable, PsiElement context) { final String text = reference.getText(); final PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(context.getProject()); + PsiExpression qualifier = reference.getQualifierExpression(); + while(qualifier != null) { + if(!(qualifier instanceof PsiReferenceExpression)) return text; + qualifier = ((PsiReferenceExpression)qualifier).getQualifierExpression(); + } final PsiVariable target = resolveHelper.resolveReferencedVariable(text, context); return variable != target ? ExpressionUtils.getQualifierOrThis(reference).getText() + "." + text : text; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForQualifiedArray.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForQualifiedArray.after.java new file mode 100644 index 000000000000..90be4b6ff70c --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForQualifiedArray.after.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test { + private String[] fields = null; + + void test() { + for (String field : this.fields) { + } + } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForQualifiedArray.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForQualifiedArray.java new file mode 100644 index 000000000000..9488b2a98ff9 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForQualifiedArray.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test { + private String[] fields = null; + + void test() { + for (int i = 0; i < this.fields.length; i++) { + String field = fields[i]; + + } + } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java index 3b53e3087ae7..fdc28f3bb474 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java @@ -30,6 +30,7 @@ public class ForCanBeForeachFixTest extends IGQuickFixesTestCase { public void testForThisClass() { doTest(); } public void testForOuterClass() { doTest(); } public void testForOuterClassIterator() { doTest(); } + public void testForQualifiedArray() { doTest(); } @Override public void setUp() throws Exception {