Redundant nullable return type: fix false positive with non-local return
#KT-41800 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
349cad7b9a
commit
de3907e8cc
+5
-4
@@ -74,12 +74,13 @@ class RedundantNullableReturnTypeInspection : AbstractKotlinInspection() {
|
|||||||
val dataFlowValueFactory = getResolutionFacade().getDataFlowValueFactory()
|
val dataFlowValueFactory = getResolutionFacade().getDataFlowValueFactory()
|
||||||
val moduleDescriptor = findModuleDescriptor()
|
val moduleDescriptor = findModuleDescriptor()
|
||||||
val languageVersionSettings = languageVersionSettings
|
val languageVersionSettings = languageVersionSettings
|
||||||
|
val returnTypes = collectDescendantsOfType<KtReturnExpression>().flatMap {
|
||||||
|
it.returnedExpression.types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings)
|
||||||
|
}
|
||||||
return if (this is KtBlockExpression) {
|
return if (this is KtBlockExpression) {
|
||||||
collectDescendantsOfType<KtReturnExpression>().flatMap {
|
returnTypes
|
||||||
it.returnedExpression.types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings)
|
returnTypes + types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
class MyClass
|
||||||
|
|
||||||
|
inline fun <T> acceptMyClass(m: (MyClass?) -> T) {}
|
||||||
|
|
||||||
|
fun one(): MyClass?<caret> {
|
||||||
|
acceptMyClass { return it }
|
||||||
|
return MyClass()
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test(str: String): String?<caret> = str.run {
|
||||||
|
return null
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
// WITH_RUNTIME
|
||||||
|
val test: Int?<caret>
|
||||||
|
get() = run {
|
||||||
|
return null
|
||||||
|
}
|
||||||
+15
@@ -8249,6 +8249,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/overridable.kt");
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/overridable.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("returnNullableFromLambdaInBlockBody.kt")
|
||||||
|
public void testReturnNullableFromLambdaInBlockBody() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInBlockBody.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("returnNullableFromLambdaInSingleExpressionBody.kt")
|
||||||
|
public void testReturnNullableFromLambdaInSingleExpressionBody() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInSingleExpressionBody.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("singleExpressionBody.kt")
|
@TestMetadata("singleExpressionBody.kt")
|
||||||
public void testSingleExpressionBody() throws Exception {
|
public void testSingleExpressionBody() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/singleExpressionBody.kt");
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/singleExpressionBody.kt");
|
||||||
@@ -8287,6 +8297,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/initializer.kt");
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/initializer.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("returnNullableFromLambdaInGetter.kt")
|
||||||
|
public void testReturnNullableFromLambdaInGetter() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/returnNullableFromLambdaInGetter.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("var.kt")
|
@TestMetadata("var.kt")
|
||||||
public void testVar() throws Exception {
|
public void testVar() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/var.kt");
|
runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/var.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user