Add inspection to detect functions with SuccessOrFailure return type

Partial implementation of KT-25621 (no quick-fixes yet)
This commit is contained in:
Mikhail Glukhikh
2018-08-21 20:56:45 +03:00
parent 48784fd95e
commit 87d750aa1c
13 changed files with 296 additions and 0 deletions
@@ -0,0 +1,92 @@
<problems>
<problem>
<file>test.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
<line>9</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
<line>15</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'incorrectCatching' returning 'SuccessOrFailure&lt;Double&gt;' without the corresponding function 'incorrect' returning 'Double'</description>
</problem>
<problem>
<file>test.kt</file>
<line>17</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'strangeCatching' returning 'SuccessOrFailure&lt;Boolean&gt;' without the corresponding function 'strange' returning 'Boolean'</description>
</problem>
<problem>
<file>test.kt</file>
<line>23</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
<line>25</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
<line>31</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'classIncorrectCatching' returning 'SuccessOrFailure&lt;Double&gt;' without the corresponding function 'classIncorrect' returning 'Double'</description>
</problem>
<problem>
<file>test.kt</file>
<line>36</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
<line>38</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
<line>40</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.coroutines.ResultIsSuccessOrFailureInspection
@@ -0,0 +1,43 @@
package kotlin
// NO (constructor)
class SuccessOrFailure<T>(val value: T?) {
fun getOrThrow(): T = value ?: throw AssertionError("")
}
// YES
fun getSuccess() = SuccessOrFailure("123")
// YES
fun getSuccessExplicit(): SuccessOrFailure<Int> = SuccessOrFailure(456)
// NO (noCatching available)
fun correctCatching() = SuccessOrFailure(true)
// NO (not SuccessOrFailure)
fun correct() = true
// YES
fun incorrectCatching() = SuccessOrFailure(3.14)
// YES
fun strangeCatching() = SuccessOrFailure(false)
// YES
fun strange() = 1
class Container {
// YES
fun classGetSuccess() = SuccessOrFailure("123")
// YES
fun classGetSuccessExplicit(): SuccessOrFailure<Int> = SuccessOrFailure(456)
// NO (noCatching available)
fun classCorrectCatching() = SuccessOrFailure(true)
// NO (not SuccessOrFailure)
fun classCorrect() = true
// YES
fun classIncorrectCatching() = SuccessOrFailure(3.14)
}
fun test() {
// YES
fun localGetSuccess() = SuccessOrFailure("123")
// YES
val anonymous = fun() = SuccessOrFailure(45)
// YES
val lambda = { SuccessOrFailure(true) }
// NO yet
fun localCatching() = SuccessOrFailure(2.72)
}