"Convert object literal to lambda" inspection: report at INFORMATION level if object literal has 'return' #KT-27116 Fixed
diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ObjectLiteralToLambdaIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ObjectLiteralToLambdaIntention.kt
index b003f7b..d5d90e5 100644
--- a/idea/src/org/jetbrains/kotlin/idea/intentions/ObjectLiteralToLambdaIntention.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ObjectLiteralToLambdaIntention.kt
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.intentions
+import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
@@ -48,7 +49,13 @@
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.types.KotlinType
-class ObjectLiteralToLambdaInspection : IntentionBasedInspection<KtObjectLiteralExpression>(ObjectLiteralToLambdaIntention::class)
+class ObjectLiteralToLambdaInspection : IntentionBasedInspection<KtObjectLiteralExpression>(ObjectLiteralToLambdaIntention::class) {
+ override fun problemHighlightType(element: KtObjectLiteralExpression): ProblemHighlightType {
+ val (_, _, singleFunction) = extractData(element) ?: return super.problemHighlightType(element)
+ if (singleFunction.bodyExpression?.anyDescendantOfType<KtReturnExpression> { true } == true) return ProblemHighlightType.INFORMATION
+ return super.problemHighlightType(element)
+ }
+}
class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiteralExpression>(
KtObjectLiteralExpression::class.java,
@@ -164,24 +171,24 @@
ShortenReferences.DEFAULT.process(replaced.containingKtFile, replaced.startOffset, endOffset)
}
}
-
- private data class Data(
- val baseTypeRef: KtTypeReference,
- val baseType: KotlinType,
- val singleFunction: KtNamedFunction
- )
-
- private fun extractData(element: KtObjectLiteralExpression): Data? {
- val objectDeclaration = element.objectDeclaration
-
- val singleFunction = objectDeclaration.declarations.singleOrNull() as? KtNamedFunction ?: return null
- if (!singleFunction.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return null
-
- val delegationSpecifier = objectDeclaration.superTypeListEntries.singleOrNull() ?: return null
- val typeRef = delegationSpecifier.typeReference ?: return null
- val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL)
- val baseType = bindingContext[BindingContext.TYPE, typeRef] ?: return null
-
- return Data(typeRef, baseType, singleFunction)
- }
}
+
+private data class Data(
+ val baseTypeRef: KtTypeReference,
+ val baseType: KotlinType,
+ val singleFunction: KtNamedFunction
+)
+
+private fun extractData(element: KtObjectLiteralExpression): Data? {
+ val objectDeclaration = element.objectDeclaration
+
+ val singleFunction = objectDeclaration.declarations.singleOrNull() as? KtNamedFunction ?: return null
+ if (!singleFunction.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return null
+
+ val delegationSpecifier = objectDeclaration.superTypeListEntries.singleOrNull() ?: return null
+ val typeRef = delegationSpecifier.typeReference ?: return null
+ val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL)
+ val baseType = bindingContext[BindingContext.TYPE, typeRef] ?: return null
+
+ return Data(typeRef, baseType, singleFunction)
+}
\ No newline at end of file
diff --git a/idea/testData/intentions/objectLiteralToLambda/inspectionData/expected.xml b/idea/testData/intentions/objectLiteralToLambda/inspectionData/expected.xml
index 581385f..479e451 100644
--- a/idea/testData/intentions/objectLiteralToLambda/inspectionData/expected.xml
+++ b/idea/testData/intentions/objectLiteralToLambda/inspectionData/expected.xml
@@ -1,23 +1,5 @@
<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>
<module>light_idea_test_case</module>
@@ -27,24 +9,6 @@
</problem>
<problem>
- <file>ReturnNoValue.kt</file>
- <line>6</line>
- <module>light_idea_test_case</module>
- <entry_point TYPE="file" FQNAME="ReturnNoValue.kt" />
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
- <description>Convert to lambda</description>
- </problem>
-
- <problem>
- <file>ReturnNotLast.kt</file>
- <line>8</line>
- <module>light_idea_test_case</module>
- <entry_point TYPE="file" FQNAME="ReturnNotLast.kt" />
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
- <description>Convert to lambda</description>
- </problem>
-
- <problem>
<file>ParametersNotUsed.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
@@ -98,24 +62,6 @@
</problem>
<problem>
- <file>ReturnsInWhen.kt</file>
- <line>8</line>
- <module>light_idea_test_case</module>
- <entry_point TYPE="file" FQNAME="ReturnsInWhen.kt" />
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
- <description>Convert to lambda</description>
- </problem>
-
- <problem>
- <file>ReturnsInIf.kt</file>
- <line>8</line>
- <module>light_idea_test_case</module>
- <entry_point TYPE="file" FQNAME="ReturnsInIf.kt" />
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
- <description>Convert to lambda</description>
- </problem>
-
- <problem>
<file>NotInRange2.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
@@ -134,23 +80,6 @@
</problem>
<problem>
- <file>QualifiedReturn.kt</file>
- <line>6</line>
- <module>light_idea_test_case</module>
- <entry_point TYPE="file" FQNAME="QualifiedReturn.kt" />
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
- <description>Convert to lambda</description>
- </problem>
-
- <problem>
- <file>SamAdapterNeededBecauseOfLabeledReturn.kt</file>
- <line>6</line>
- <module>light_idea_test_case</module>
- <entry_point TYPE="file" FQNAME="SamAdapterNeededBecauseOfLabeledReturn.kt" />
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
- <description>Convert to lambda</description>
- </problem>
- <problem>
<file>ThisReference.kt</file>
<line>12</line>
<module>light_idea_test_case</module>