You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, it does not apply to while loops, and maybe it should, and thus be renamed to ModifyCollectionInLoop
For example, I would like this POC unit test to cause my build to fail when this errorprone rule (or another like it) is active
/** Expect {@link ConcurrentModificationException} when removing an element directly from inside the loop. **/@TestvoidlistRemoveInvalid() {
List<String> list = newArrayList<>();
list.add("first");
list.add("second");
list.add("third");
AssertionsForClassTypes.assertThatCode(() -> {
Iterator<String> iterator = list.iterator();
//noinspection WhileLoopReplaceableByForEachwhile (iterator.hasNext()) {
Stringitem = iterator.next();
list.remove(item);
}
}).doesNotThrowAnyExceptionExcept(ConcurrentModificationException.class);
}
Probably why no-one else has seen this is that its common to follow Intellij's prompt to convert the while loop to a for, but I have reason not to do that.
The text was updated successfully, but these errors were encountered:
Despite its name the ModifyCollectionInEnhancedForLoop rule applies to classic for loops as well as enhanced for loops.
As far as I can tell it doesn't.
I agree with the FR part of this issue, though. However, it gets progressively harder to implement, which I'd suspect is a fair bit of the reason it only matches enhanced for loops. :)
Is the motivation of your example that the author should have written iterator.remove() rather than list.remove(item)?
Despite its name the ModifyCollectionInEnhancedForLoop rule applies to classic for loops as well as enhanced for loops.
However, it does not apply to while loops, and maybe it should, and thus be renamed to
ModifyCollectionInLoop
For example, I would like this POC unit test to cause my build to fail when this errorprone rule (or another like it) is active
Probably why no-one else has seen this is that its common to follow Intellij's prompt to convert the while loop to a for, but I have reason not to do that.
The text was updated successfully, but these errors were encountered: