diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html
index 4d408caa2cf7..79312654409e 100644
--- a/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html
+++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html
@@ -1,16 +1,16 @@
Reports for loops which iterate
-over collections or arrays, and can be replaced with the foreach iteration syntax, available
-in Java 5 and newer.
+over collections or arrays, and can be replaced with an enhanced for loop (i.e. the foreach iteration syntax).
+
Use the first checkbox below to find loops involving list.get(index) calls.
-These loops generally can be replaced with the foreach loops, unless they modify underlying list in the process, e.g. by calling list.remove(index).
-If latter is the case, foreach form of loop may throw ConcurrentModificationException.
+These loops generally can be replaced with an enhanced for loop, unless they modify underlying list in the process, e.g. by calling list.remove(index).
+If latter is the case, the enhanced for loop may throw ConcurrentModificationException.
-Use the second checkbox below to ignore collections which do not have type parameters. This avoids the creating of
-foreach loop variables of type java.lang.Object and casts by the quickfix
+Use the second checkbox below to ignore collections which do not have type parameters. This prevents the creation of
+enhanced for loop variables of type java.lang.Object and the insertion of casts where the loop variable is used.
This inspection only reports if the language level of the project or module is 5 or higher
diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/WhileCanBeForeach.html b/plugins/InspectionGadgets/src/inspectionDescriptions/WhileCanBeForeach.html
index b2e8a31b6a98..787aeb938f26 100644
--- a/plugins/InspectionGadgets/src/inspectionDescriptions/WhileCanBeForeach.html
+++ b/plugins/InspectionGadgets/src/inspectionDescriptions/WhileCanBeForeach.html
@@ -1,7 +1,9 @@
Reports while loops which iterate
-over collections, and can be replaced with the foreach iteration syntax. Example:
+over collections, and can be replaced with an enhanced for loop (i.e. foreach iteration syntax).
+
+Example:
Iterator it = c.iterator();
while(it.hasNext()) {
@@ -11,9 +13,7 @@ over collections, and can be replaced with the foreach iteration syntax.
Can be replaced with:
- Iterator it = c.iterator();
- while(it.hasNext()) {
- Object obj = it.next();
+ for (Object obj : c) {
System.out.println(obj);
}
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/foreach/ForCanBeForEach.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/foreach/ForCanBeForEach.java
index daf20c73747e..64c6b372c24c 100644
--- a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/foreach/ForCanBeForEach.java
+++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/foreach/ForCanBeForEach.java
@@ -5,13 +5,13 @@ import java.util.*;
public class ForCanBeForEach {
public void foo(int[] is) {
- for (int i = 0; i < is.length; i++) {
+ for (int i = 0; i < is.length; i++) {
System.out.println(is[i]);
}
}
public void test(Collection bars){
- for(Iterator it = bars.iterator(); it .hasNext();){
+ for(Iterator it = bars.iterator(); it .hasNext();){
final List bar = it.next();
bar.size();
}
@@ -20,7 +20,7 @@ public class ForCanBeForEach {
public int foo(){
final int[] ints = new int[3];
int total = 0;
- for(int i = 0; i < ints.length; i++){
+ for(int i = 0; i < ints.length; i++){
final int j = ints[i];
total += j;
}
@@ -30,7 +30,7 @@ public class ForCanBeForEach {
public int bar(){
final int[] ints = new int[3];
int total = 0;
- for(int i = 0; i < ints.length; i++){
+ for(int i = 0; i < ints.length; i++){
total += ints[i];
}
return total;
@@ -39,7 +39,7 @@ public class ForCanBeForEach {
public int baz(){
int total = 0;
final List ints = new ArrayList();
- for(Iterator iterator = ints.iterator(); iterator.hasNext();){
+ for(Iterator iterator = ints.iterator(); iterator.hasNext();){
final Integer value = (Integer) iterator.next();
total += value.intValue();
}
@@ -49,7 +49,7 @@ public class ForCanBeForEach {
public int bazoom(){
int total = 0;
final List ints = new ArrayList();
- for(Iterator iterator = ints.iterator(); iterator.hasNext();){
+ for(Iterator iterator = ints.iterator(); iterator.hasNext();){
final Integer value = iterator.next();
total += value.intValue();
}
@@ -59,7 +59,7 @@ public class ForCanBeForEach {
public int wildBazoom(){
int total = 0;
final List extends Integer> ints = new ArrayList();
- for(Iterator extends Integer> iterator = ints.iterator();
+ for(Iterator extends Integer> iterator = ints.iterator();
iterator.hasNext();){
final Integer value = iterator.next();
total += value.intValue();
@@ -79,7 +79,7 @@ public class ForCanBeForEach {
Map m = new HashMap();
m.put("123", 123);
m.put("456", 456);
- for(Iterator> iterator = m.entrySet()
+ for(Iterator> iterator = m.entrySet()
.iterator(); iterator.hasNext();){
Map.Entry entry = iterator.next();
System.out.println(entry.getKey() + "=" + entry.getValue());
@@ -102,7 +102,7 @@ public class ForCanBeForEach {
public void boom2(){
OuterClass.UnnecessaryEnumModifier2Inspection[] inners = new OuterClass.UnnecessaryEnumModifier2Inspection[3];
- for(int i = 0; i < inners.length; i++){
+ for(int i = 0; i < inners.length; i++){
OuterClass.UnnecessaryEnumModifier2Inspection inner = inners[i];
System.out.println(inner);
}
@@ -124,7 +124,7 @@ public class ForCanBeForEach {
}
public void quickFixBoom(List numbers) {
- for (int i = 0; i < (numbers.size()); i++) {
+ for (int i = 0; i < (numbers.size()); i++) {
System.out.println("numbers[i]: " + numbers.get(i));
}
}
@@ -180,7 +180,7 @@ public class ForCanBeForEach {
void sizeInVariable(List ls) {
int size = ls.size();
- for (int i = (0); (i < (size)); (i)++) {
+ for (int i = (0); (i < (size)); (i)++) {
Object o = ls.get(i);
System.out.println("o = " + o);
}
@@ -189,7 +189,7 @@ public class ForCanBeForEach {
class X extends ArrayList {
void foo() {
- for (int i = 0; i < size(); i++) {
+ for (int i = 0; i < size(); i++) {
this.get(i);
}
}
@@ -206,7 +206,7 @@ public class ForCanBeForEach {
int strange() {
int total = 0;
final List ints = new ArrayList();
- for (ListIterator l = ints.listIterator(); l.hasNext(); ) {
+ for (ListIterator l = ints.listIterator(); l.hasNext(); ) {
System.out.println(l.next());
}
return total;
@@ -235,10 +235,10 @@ public class ForCanBeForEach {
}
void indexedList(List l) {
- for (int i = 0, max = l.size(); i < max; i++) {
+ for (int i = 0, max = l.size(); i < max; i++) {
System.out.println(l.get(i));
}
- for (int i = (0), max = (l.size()); ((max) > (i)); (i)++) {
+ for (int i = (0), max = (l.size()); ((max) > (i)); (i)++) {
System.out.println(l.get(i)); }
for (int i = 0; i < l.size(); i++) {}
}
@@ -248,14 +248,14 @@ public class ForCanBeForEach {
}
static class User {{
String[] strings = Constants.STRINGS;
- for (int i = 0, length = strings.length; i < length; i++) { // should warn here
+ for (int i = 0, length = strings.length; i < length; i++) { // should warn here
String s = strings[i];
System.out.println(s);
}
}}
public void food(int[] is) {
- for (int i = 0; is.length > i; i++) {
+ for (int i = 0; is.length > i; i++) {
System.out.println(is[i]);
}
for (int i = 0, j = 10; i < is.length; i++) {
@@ -281,7 +281,7 @@ public class ForCanBeForEach {
class XX {
void m(T[] ts) {
- for (int i = 0; i < ts.length; i++) {
+ for (int i = 0; i < ts.length; i++) {
System.out.println(ts[i]);
}
}
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/while_can_be_foreach/WhileCanBeForeach.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/while_can_be_foreach/WhileCanBeForeach.java
index bb389351e3b5..fcf4e88d0c7d 100644
--- a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/while_can_be_foreach/WhileCanBeForeach.java
+++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/while_can_be_foreach/WhileCanBeForeach.java
@@ -8,7 +8,7 @@ public class WhileCanBeForeach {
int total = 0;
final List ints = new ArrayList();
Iterator iterator = ints.iterator();
- while ( iterator.hasNext()) {
+ while ( iterator.hasNext()) {
total += (Integer) iterator.next();
}
return total;
@@ -18,7 +18,7 @@ public class WhileCanBeForeach {
int total = 0;
final List ints = new ArrayList();
Iterator iterator = ints.iterator();
- while ( iterator.hasNext()) {
+ while ( iterator.hasNext()) {
total += (Integer) iterator.next();
}
iterator = ints.iterator(); // write use here
@@ -38,7 +38,7 @@ public class WhileCanBeForeach {
void no(Collection pbps, Map tracksToPBP) {
final Iterator pbpsIt = pbps.iterator();
- while (pbpsIt.hasNext()) {
+ while (pbpsIt.hasNext()) {
final String pbp = (String) pbpsIt.next();
final Iterator trackIt = it();
while (trackIt.hasNext()) {
@@ -70,7 +70,7 @@ public class WhileCanBeForeach {
int total = 0;
final List ints = new ArrayList();
ListIterator iterator = ints.listIterator();
- while ( iterator.hasNext()) {
+ while ( iterator.hasNext()) {
final Object next = iterator.next();
total += (Integer) next;
}