FindFirstMigration: use ifPresent when stream element type is primitive and the final statement is an assignment

This case is possible only if we have no non-final locals updated in the loop, so the assignment updates field or array element.
Using common assignment scenario may not work as primitive optionals lack map/filter operations
Fixes IDEA-200209 "Replace with findFirst" produces incompilable code
This commit is contained in:
Tagir Valeev
2018-10-10 15:55:53 +07:00
parent c9ccaee0a6
commit cee88f887b
3 changed files with 44 additions and 1 deletions
@@ -0,0 +1,19 @@
// "Replace with findFirst()" "true"
import java.util.stream.IntStream;
class Lookup {
boolean matches(char[] lookup) {}
}
// IDEA-200209
class C {
private Lookup[] lookbehindFormats;
private char[] lookupChars;
private Lookup lookbehindFormat;
{
IntStream.range(0, lookbehindFormats.length).filter(i -> lookbehindFormats[i].matches(lookupChars)).findFirst().ifPresent(i -> lookbehindFormat = lookbehindFormats[i]);
}
}
@@ -0,0 +1,22 @@
// "Replace with findFirst()" "true"
class Lookup {
boolean matches(char[] lookup) {}
}
// IDEA-200209
class C {
private Lookup[] lookbehindFormats;
private char[] lookupChars;
private Lookup lookbehindFormat;
{
for (int i = 0; i < lookbehin<caret>dFormats.length; i++) {
if (lookbehindFormats[i].matches(lookupChars)) {
lookbehindFormat = lookbehindFormats[i];
break;
}
}
}
}