Object literal can be converted to lambda: report if "return" is only one and is in the last statement

#KT-29763 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-14 10:34:40 +09:00
committed by Mikhail Glukhikh
parent 7c3c21790b
commit cd13648906
2 changed files with 21 additions and 1 deletions
@@ -51,7 +51,9 @@ import org.jetbrains.kotlin.types.KotlinType
class ObjectLiteralToLambdaInspection : IntentionBasedInspection<KtObjectLiteralExpression>(ObjectLiteralToLambdaIntention::class) {
override fun problemHighlightType(element: KtObjectLiteralExpression): ProblemHighlightType {
val (_, baseType, singleFunction) = extractData(element) ?: return super.problemHighlightType(element)
if (singleFunction.bodyExpression?.anyDescendantOfType<KtReturnExpression> { true } == true) return ProblemHighlightType.INFORMATION
val bodyBlock = singleFunction.bodyBlockExpression
val lastStatement = bodyBlock?.statements?.lastOrNull()
if (bodyBlock?.anyDescendantOfType<KtReturnExpression> { it != lastStatement } == true) return ProblemHighlightType.INFORMATION
val valueArgument = element.parent as? KtValueArgument
val call = valueArgument?.getStrictParentOfType<KtCallExpression>()
@@ -1,4 +1,22 @@
<problems>
<problem>
<file>StatementAndReturn.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="StatementAndReturn.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
<description>Convert to lambda</description>
</problem>
<problem>
<file>SingleReturn.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="SingleReturn.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
<description>Convert to lambda</description>
</problem>
<problem>
<file>Simple.kt</file>
<line>6</line>