"Convert object literal to lambda" inspection: report at INFORMATION level if object literal has 'return' #KT-27116 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-10-04 18:38:52 +09:00
committed by Mikhail Glukhikh
parent 23734bae3e
commit f4a637a72e
2 changed files with 28 additions and 92 deletions
@@ -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.ImplicitClassReceiver
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 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
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)
}
@@ -1,22 +1,4 @@
<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>
@@ -26,24 +8,6 @@
<description>Convert to lambda</description>
</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>
@@ -97,24 +61,6 @@
<description>Convert to lambda</description>
</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>
@@ -133,23 +79,6 @@
<description>Convert to lambda</description>
</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>