Commit cee88f88 authored by Tagir Valeev's avatar Tagir Valeev
Browse files

FindFirstMigration: use ifPresent when stream element type is primitive and...

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
parent c9ccaee0
Branches unavailable Tags unavailable
No related merge requests found
Showing with 44 additions and 1 deletion
+44 -1
......@@ -42,7 +42,9 @@ class FindFirstMigration extends BaseStreamApiMigration {
PsiStatement[] statements = tb.getStatements();
if (statements.length != 2) return null;
PsiAssignmentExpression assignment = ExpressionUtils.getAssignment(statements[0]);
if (assignment == null) {
if (assignment == null || tb.getVariable().getType() instanceof PsiPrimitiveType) {
// if we found an assignment with primitive stream variable, then we are not assigning to local variable
// (see StreamApiMigrationInspection#findMigrationForBreak), thus it could be handled via ifPresent()
if(!(statements[0] instanceof PsiExpressionStatement)) return null;
PsiExpression expression = ((PsiExpressionStatement)statements[0]).getExpression();
return ct.replaceAndRestoreComments(
......
// "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]);
}
}
\ No newline at end of file
// "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;
}
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment