Do not suggest "Redundant Unit return type" for Nothing-typed expression
So #KT-20261 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
d87c0b164f
commit
1c7d66c803
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.psi.KtVisitorVoid
|
|||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||||
|
|
||||||
class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() {
|
class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() {
|
||||||
@@ -43,7 +44,9 @@ class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() {
|
|||||||
if (!function.hasBlockBody()) {
|
if (!function.hasBlockBody()) {
|
||||||
val bodyExpression = function.bodyExpression
|
val bodyExpression = function.bodyExpression
|
||||||
if (bodyExpression != null) {
|
if (bodyExpression != null) {
|
||||||
val resolvedCall = bodyExpression.getResolvedCall(bodyExpression.analyze(BodyResolveMode.PARTIAL))
|
val bodyContext = bodyExpression.analyze(BodyResolveMode.PARTIAL)
|
||||||
|
if (bodyContext.getType(bodyExpression)?.isNothing() == true) return
|
||||||
|
val resolvedCall = bodyExpression.getResolvedCall(bodyContext)
|
||||||
if (resolvedCall != null) {
|
if (resolvedCall != null) {
|
||||||
if (resolvedCall.candidateDescriptor.returnType?.isUnit() != true) return
|
if (resolvedCall.candidateDescriptor.returnType?.isUnit() != true) return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,10 @@ fun bar(): Unit {
|
|||||||
|
|
||||||
fun baz(): Unit = println("ok")
|
fun baz(): Unit = println("ok")
|
||||||
|
|
||||||
fun f1(): Int = 1
|
fun f1(): Int = 1
|
||||||
|
|
||||||
|
fun f2(): Unit {
|
||||||
|
throw UnsupportedOperationException("")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun f3(): Unit = throw UnsupportedOperationException("")
|
||||||
@@ -23,6 +23,14 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'Unit' return type</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'Unit' return type</problem_class>
|
||||||
<description>Redundant Unit return type</description>
|
<description>Redundant Unit return type</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>Test.kt</file>
|
||||||
|
<line>12</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/Test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'Unit' return type</problem_class>
|
||||||
|
<description>Redundant Unit return type</description>
|
||||||
|
</problem>
|
||||||
<problem>
|
<problem>
|
||||||
<file>WithLambda.kt</file>
|
<file>WithLambda.kt</file>
|
||||||
<line>11</line>
|
<line>11</line>
|
||||||
|
|||||||
Reference in New Issue
Block a user