mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
[java-inspections] ToArrayTerminal: allow reusing a target collection in another branch only when return is on the same level
Fixes IDEA-304740 FuseStreamOperationsInspection fix produces red code GitOrigin-RevId: b9a169e54bae1de448194b8debb39f79904ceb90
This commit is contained in:
committed by
intellij-monorepo-bot
parent
c480c032ff
commit
98a4b2d977
@@ -850,8 +850,13 @@ class CollectMigration extends BaseStreamApiMigration {
|
||||
if (toArrayCandidate == null) return null;
|
||||
PsiReferenceExpression methodExpression = toArrayCandidate.getMethodExpression();
|
||||
if (!"toArray".equals(methodExpression.getReferenceName())) return null;
|
||||
if (!(PsiUtil.skipParenthesizedExprUp(toArrayCandidate.getParent()) instanceof PsiReturnStatement) &&
|
||||
usages.stream().anyMatch(usage -> !PsiTreeUtil.isAncestor(toArrayCandidate, usage, false))) {
|
||||
/* We want to allow reusing the same empty collection in another branch of code after return.
|
||||
* However, in this case, return should be on the same level as the stream itself.
|
||||
* See beforeToArrayInBranch.java and beforeToArrayReusedCollection.java tests.
|
||||
*/
|
||||
if ((!(PsiUtil.skipParenthesizedExprUp(toArrayCandidate.getParent()) instanceof PsiReturnStatement stmt)
|
||||
|| stmt.getParent() != element.getParent()) &&
|
||||
ContainerUtil.exists(usages, usage -> !PsiTreeUtil.isAncestor(toArrayCandidate, usage, false))) {
|
||||
return null;
|
||||
}
|
||||
PsiLocalVariable var = tryCast(PsiUtil.skipParenthesizedExprUp(toArrayCandidate.getParent()), PsiLocalVariable.class);
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// "Fuse 'toArray' into the Stream API chain" "false"
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
class Test {
|
||||
public Object[] getArray(String[] input, boolean f) {
|
||||
List<String> list = Arrays.stream(input)
|
||||
.filter(Objects::nonNull)
|
||||
.<caret>collect(Collectors.toList());
|
||||
String[] data = new String[] {};
|
||||
if (f) {
|
||||
return list.toArray();
|
||||
}
|
||||
list.add("hello");
|
||||
return list.toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user