CallHandler<CallChainSimplification> () { return CallHandler.of(BASE_STREAM_MAP, call -> { PsiLambdaExpression lambda = tryCast(skipParenthesizedExprDown(call.getArgumentList().getExpressions()[0]), PsiLambdaExpression.class); if (lambda == null) return null; PsiParameter[] parameters = lambda.getParameterList().getParameters(); if (parameters.length != 1) return null; PsiCodeBlock block = tryCast(lambda.getBody(), PsiCodeBlock.class); if (block == null) return null; PsiStatement[] statements = block.getStatements(); if (statements.length <= 1) return null; PsiReturnStatement returnStatement = tryCast(ArrayUtil.getLastElement(statements), PsiReturnStatement.class); PsiParameter parameter = parameters[0]; if (returnStatement == null || !ExpressionUtils.isReferenceTo(returnStatement.getReturnValue(), parameter)) return null; if (VariableAccessUtils.variableIsAssigned(parameter)) return null; if (Arrays.stream(statements, 0, statements.length - 1).anyMatch(ControlFlowUtils::containsReturn)) return null; return new ReplaceWithPeekFix(); }); }