Repository: JetBrains/kotlin
File: idea/src/org/jetbrains/kotlin/idea/inspections/RedundantWithInspection.kt (Kotlin)

Code before:
                )
            }
        })
}

private fun KtValueArgument.lambdaExpression(): KtLambdaExpression? =
    (this as? KtLambdaArgument)?.getLambdaExpression() ?: this.getArgumentExpression() as? KtLambdaExpression

private fun KtCallExpression.isDeclarationInitializer(): Boolean =
    KtPsiUtil.deparenthesize(this.getStrictParentOfType<KtDeclarationWithInitializer>()?.initializer) == this

private class RemoveRedundantWithFix : LocalQuickFix {
    override fun getName() = KotlinBundle.message("remove.redundant.with.fix.text")

    override fun getFamilyName() = name

    override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
        val callExpression = descriptor.psiElement.parent as? KtCallExpression ?: return
        val lambdaBody = callExpression.valueArguments.getOrNull(1)?.lambdaExpression()?.bodyExpression ?: return
        val singleReturnedExpression = (lambdaBody.statements.singleOrNull() as? KtReturnExpression)?.returnedExpression
        val newElement = if (singleReturnedExpression != null && callExpression.isDeclarationInitializer()) {
            singleReturnedExpression
        } else {
            lambdaBody
        }
        val replaced = callExpression.replaced(newElement)
        replaced.findExistingEditor()?.moveCaret(replaced.startOffset)
    }
}


Review comment (suggestion):
@t-kameyama would it be also possible to include the case mentioned in `testData/inspectionsLocal/redundantWith/notApplicable_inFunctionBodyWithReturn.kt`?
```
val lambdaExpression = callExpression.valueArguments.getOrNull(1)?.lambdaExpression()
KtPsiUtil.skipSiblingsBackwardByPredicate(callExpression) { element -> element.text != "=" }?.delete()
val replaced = callExpression.replaced(lambdaExpression!!)
```

Code after:
        })
}

private fun KtValueArgument.lambdaExpression(): KtLambdaExpression? =
    (this as? KtLambdaArgument)?.getLambdaExpression() ?: this.getArgumentExpression() as? KtLambdaExpression

private class RemoveRedundantWithFix : LocalQuickFix {
    override fun getName() = KotlinBundle.message("remove.redundant.with.fix.text")

    override fun getFamilyName() = name

    override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
        val callExpression = descriptor.psiElement.parent as? KtCallExpression ?: return
        val lambdaExpression = callExpression.valueArguments.getOrNull(1)?.lambdaExpression() ?: return
        val lambdaBody = lambdaExpression.bodyExpression ?: return
        val declaration = callExpression.getStrictParentOfType<KtDeclarationWithBody>()
        val replaced = if (declaration?.equalsToken != null && KtPsiUtil.deparenthesize(declaration.bodyExpression) == callExpression) {
            val singleReturnedExpression = (lambdaBody.statements.singleOrNull() as? KtReturnExpression)?.returnedExpression
            if (singleReturnedExpression != null) {
                callExpression.replaced(singleReturnedExpression)
            } else {
                declaration.equalsToken?.delete()
                declaration.bodyExpression?.replaced(KtPsiFactory(project).createSingleStatementBlock(lambdaBody))
            }
        } else {
            callExpression.replaced(lambdaBody)
        }
        if (replaced != null) {
            replaced.findExistingEditor()?.moveCaret(replaced.startOffset)
        }
    }
}