void (PsiExpression expression, PsiElementFactory factory, boolean keepParameterTypes) { if (!(expression instanceof PsiLambdaExpression lambda)) return; PsiParameter[] parameters = lambda.getParameterList().getParameters(); PsiElement body = lambda.getBody(); if (body == null) return; if (LambdaCanBeMethodReferenceInspection.replaceLambdaWithMethodReference(lambda) == lambda) { PsiParameter parameter = parameters[0]; String name = suggestVarName(parameter); if (name != null) { Collection<PsiReferenceExpression> references = PsiTreeUtil.collectElementsOfType(body, PsiReferenceExpression.class); StreamEx.of(references).filter(ref -> ref.isReferenceTo(parameter)).map(PsiJavaCodeReferenceElement::getReferenceNameElement) .nonNull().forEach(nameElement -> nameElement.replace(factory.createIdentifier(name))); parameter.setName(name); } if (!keepParameterTypes) { RemoveRedundantParameterTypesFix.removeLambdaParameterTypesIfPossible(lambda); } } }