IG: foreach -> enhanced 'for'

GitOrigin-RevId: 2c880b5c7bfbd53dc49226905d57a4b93536d462
This commit is contained in:
Bas Leijdekkers
2019-10-15 11:04:20 +00:00
committed by intellij-monorepo-bot
parent 1f472af6dc
commit 5121d910d2
4 changed files with 32 additions and 32 deletions
@@ -1,16 +1,16 @@
<html>
<body>
Reports <b>for</b> loops which iterate
over collections or arrays, and can be replaced with the <b>foreach</b> iteration syntax, available
in Java 5 and newer.
over collections or arrays, and can be replaced with an enhanced <b>for</b> loop (i.e. the foreach iteration syntax).
<!-- 'foreach' still used in this description so that the inspection continues to be found using this keyword -->
<!-- tooltip end -->
<p>
Use the first checkbox below to find loops involving <b>list.get(index)</b> calls.
These loops generally can be replaced with the <b>foreach</b> loops, unless they modify underlying list in the process, e.g. by calling <b>list.remove(index)</b>.
If latter is the case, <b>foreach</b> form of loop may throw <b>ConcurrentModificationException</b>.
These loops generally can be replaced with an enhanced <b>for</b> loop, unless they modify underlying list in the process, e.g. by calling <b>list.remove(index)</b>.
If latter is the case, the enhanced <b>for</b> loop may throw <b>ConcurrentModificationException</b>.
<p>
Use the second checkbox below to ignore collections which do not have type parameters. This avoids the creating of
foreach loop variables of type <b>java.lang.Object</b> 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 <b>for</b> loop variables of type <b>java.lang.Object</b> and the insertion of casts where the loop variable is used.
<p>This inspection only reports if the language level of the project or module is 5 or higher</p>
<p>
@@ -1,7 +1,9 @@
<html>
<body>
Reports <b>while</b> loops which iterate
over collections, and can be replaced with the <b>foreach</b> iteration syntax. Example:
over collections, and can be replaced with an enhanced <b>for</b> loop (i.e. foreach iteration syntax).
<!-- 'foreach' still used in this description so that the inspection continues to be found using this keyword -->
Example:
<pre>
Iterator it = c.iterator();
while(it.hasNext()) {
@@ -11,9 +13,7 @@ over collections, and can be replaced with the <b>foreach</b> iteration syntax.
</pre>
Can be replaced with:
<pre>
Iterator it = c.iterator();
while(it.hasNext()) {
Object obj = it.next();
for (Object obj : c) {
System.out.println(obj);
}
</pre>
@@ -5,13 +5,13 @@ import java.util.*;
public class ForCanBeForEach {
public void foo(int[] is) {
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0; i < is.length; i++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (int i = 0; i < is.length; i++) {
System.out.println(is[i]);
}
}
public void test(Collection bars){
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(Iterator<List> it = bars.iterator(); it .hasNext();){
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(Iterator<List> 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;
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(int i = 0; i < ints.length; i++){
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(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;
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(int i = 0; i < ints.length; i++){
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(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();
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(Iterator iterator = ints.iterator(); iterator.hasNext();){
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(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<Integer> ints = new ArrayList<Integer>();
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(Iterator<Integer> iterator = ints.iterator(); iterator.hasNext();){
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(Iterator<Integer> 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<Integer>();
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(Iterator<? extends Integer> iterator = ints.iterator();
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(Iterator<? extends Integer> iterator = ints.iterator();
iterator.hasNext();){
final Integer value = iterator.next();
total += value.intValue();
@@ -79,7 +79,7 @@ public class ForCanBeForEach {
Map<String, Integer> m = new HashMap<String, Integer>();
m.put("123", 123);
m.put("456", 456);
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(Iterator<Map.Entry<String, Integer>> iterator = m.entrySet()
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(Iterator<Map.Entry<String, Integer>> iterator = m.entrySet()
.iterator(); iterator.hasNext();){
Map.Entry<String, Integer> 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];
<warning descr="'for' loop replaceable with 'foreach'">for</warning>(int i = 0; i < inners.length; i++){
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning>(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) {
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0; i < (numbers.size()); i++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (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();
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = (0); (i < (size)); (i)++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (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() {
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0; i < size(); i++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (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();
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (ListIterator l = ints.listIterator(); l.hasNext(); ) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (ListIterator l = ints.listIterator(); l.hasNext(); ) {
System.out.println(l.next());
}
return total;
@@ -235,10 +235,10 @@ public class ForCanBeForEach {
}
void indexedList(List<String> l) {
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0, max = l.size(); i < max; i++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (int i = 0, max = l.size(); i < max; i++) {
System.out.println(l.get(i));
}
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = (0), max = (l.size()); ((max) > (i)); (i)++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (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;
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0, length = strings.length; i < length; i++) { // should warn here
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (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) {
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0; is.length > i; i++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (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<T> {
void m(T[] ts) {
<warning descr="'for' loop replaceable with 'foreach'">for</warning> (int i = 0; i < ts.length; i++) {
<warning descr="'for' loop replaceable with enhanced 'for'">for</warning> (int i = 0; i < ts.length; i++) {
System.out.println(ts[i]);
}
}
@@ -8,7 +8,7 @@ public class WhileCanBeForeach {
int total = 0;
final List ints = new ArrayList();
Iterator iterator = ints.iterator();
<warning descr="'while' loop replaceable with 'foreach'">while</warning> ( iterator.hasNext()) {
<warning descr="'while' loop replaceable with enhanced 'for'">while</warning> ( 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();
<warning descr="'while' loop replaceable with 'foreach'">while</warning> ( iterator.hasNext()) {
<warning descr="'while' loop replaceable with enhanced 'for'">while</warning> ( 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();
<warning descr="'while' loop replaceable with 'foreach'">while</warning> (pbpsIt.hasNext()) {
<warning descr="'while' loop replaceable with enhanced 'for'">while</warning> (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();
<warning descr="'while' loop replaceable with 'foreach'">while</warning> ( iterator.hasNext()) {
<warning descr="'while' loop replaceable with enhanced 'for'">while</warning> ( iterator.hasNext()) {
final Object next = iterator.next();
total += (Integer) next;
}