Modified IfThenToElvis to not apply when using '!!' would be better

This commit is contained in:
Zack Grannan
2014-04-07 05:35:23 -07:00
committed by Zalim Bashorov
parent 9f63f7c488
commit e5054a4a54
6 changed files with 40 additions and 2 deletions
@@ -29,6 +29,8 @@ import org.jetbrains.jet.plugin.intentions.branchedTransformations.isNotNullExpr
import org.jetbrains.jet.plugin.intentions.branchedTransformations.replace
import org.jetbrains.jet.plugin.intentions.branchedTransformations.inlineLeftSideIfApplicableWithPrompt
import org.jetbrains.jet.plugin.intentions.branchedTransformations.isStableVariable
import org.jetbrains.jet.plugin.intentions.branchedTransformations.throwsNullPointerExceptionWithNoArguments
import org.jetbrains.jet.lang.psi.JetThrowExpression
public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>("if.then.to.elvis", javaClass()) {
@@ -42,8 +44,15 @@ public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>
if (expression == null || !expression.isStableVariable()) return false
return when (condition.getOperationToken()) {
JetTokens.EQEQ -> thenClause.isNotNullExpression() && elseClause.evaluatesTo(expression)
JetTokens.EXCLEQ -> elseClause.isNotNullExpression() && thenClause.evaluatesTo(expression)
JetTokens.EQEQ ->
thenClause.isNotNullExpression() && elseClause.evaluatesTo(expression) &&
!(thenClause is JetThrowExpression && thenClause.throwsNullPointerExceptionWithNoArguments())
JetTokens.EXCLEQ ->
elseClause.isNotNullExpression() && thenClause.evaluatesTo(expression) &&
!(elseClause is JetThrowExpression && elseClause.throwsNullPointerExceptionWithNoArguments())
else -> false
}
}
@@ -79,4 +79,12 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'.</problem_class>
<description>Replace 'if' expression with elvis expression</description>
</problem>
<problem>
<file>throwsNPEwithArgument.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/throwsNPEwithArgument.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>Replace 'if' expression with elvis expression</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun main(args: Array<String>) {
val t: String? = "abc"
if (t != null<caret>) t else throw KotlinNullPointerException()
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun main(args: Array<String>) {
val t: String? = "abc"
if (t == null<caret>) throw NullPointerException() else t
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun main(args: Array<String>) {
val t: String? = "abc"
if (t != null<caret>) t else throw NullPointerException("'t' must not be null")
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun main(args: Array<String>) {
"abc" ?: throw NullPointerException("'t' must not be null")
}