Fix ModifyCollectionInEnhancedForLoop warning flagged by error prone

Running recent error prone version complaining on that code:

BucketedCallback.java:73: warning: [ModifyCollectionInEnhancedForLoop]
Modifying a collection while iterating over it in a loop may cause a
ConcurrentModificationException to be thrown or lead to undefined behavior.
        entries.remove(e);
                      ^
(see https://errorprone.info/bugpattern/ModifyCollectionInEnhancedForLoop)

Bug: Issue 12677
Change-Id: I0fcd397950bb8ab2ef4069a9ce6158edc9fb129c
This commit is contained in:
David Ostrovsky
2020-05-21 15:31:42 +02:00
committed by David Pursehouse
parent 849bdfb392
commit 05aa0864dc

View File

@@ -21,8 +21,8 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.gerrit.metrics.Description;
import com.google.gerrit.metrics.Field;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/** Abstract callback metric broken down into buckets. */
@@ -67,10 +67,11 @@ abstract class BucketedCallback<V> implements BucketedMetric {
}
void doPrune() {
Set<Map.Entry<Object, BucketedCallback<V>.ValueGauge>> entries = cells.entrySet();
for (Map.Entry<Object, ValueGauge> e : entries) {
Iterator<Map.Entry<Object, ValueGauge>> it = cells.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Object, ValueGauge> e = it.next();
if (!e.getValue().set) {
entries.remove(e);
it.remove();
registry.remove(submetric(e.getKey()));
}
}