From e5054a4a54b172ce08d5c6ff2b1489704c4e461a Mon Sep 17 00:00:00 2001 From: Zack Grannan Date: Mon, 7 Apr 2014 05:35:23 -0700 Subject: [PATCH] Modified IfThenToElvis to not apply when using '!!' would be better --- .../intentions/IfThenToElvisIntention.kt | 13 +++++++++++-- .../ifThenToElvis/inspectionData/expected.xml | 8 ++++++++ .../notApplicableForSimpleKotlinNPE.kt | 6 ++++++ .../ifThenToElvis/notApplicableForSimpleNPE.kt | 6 ++++++ .../branched/ifThenToElvis/throwsNPEwithArgument.kt | 5 +++++ .../ifThenToElvis/throwsNPEwithArgument.kt.after | 4 ++++ 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleKotlinNPE.kt create mode 100644 idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleNPE.kt create mode 100644 idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt create mode 100644 idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt.after diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt index 28287cf530e..2d510e90bb4 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt @@ -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("if.then.to.elvis", javaClass()) { @@ -42,8 +44,15 @@ public class IfThenToElvisIntention : JetSelfTargetingIntention 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 } } diff --git a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml index 2198f4442e1..d514b25da11 100644 --- a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml +++ b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml @@ -79,4 +79,12 @@ If-Then foldable to '?:'. Replace 'if' expression with elvis expression + + throwsNPEwithArgument.kt + 4 + light_idea_test_case + + If-Then foldable to '?:' + Replace 'if' expression with elvis expression + diff --git a/idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleKotlinNPE.kt b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleKotlinNPE.kt new file mode 100644 index 00000000000..6a1e90b294e --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleKotlinNPE.kt @@ -0,0 +1,6 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +fun main(args: Array) { + val t: String? = "abc" + if (t != null) t else throw KotlinNullPointerException() +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleNPE.kt b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleNPE.kt new file mode 100644 index 00000000000..ef6fc01b6dd --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForSimpleNPE.kt @@ -0,0 +1,6 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +fun main(args: Array) { + val t: String? = "abc" + if (t == null) throw NullPointerException() else t +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt b/idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt new file mode 100644 index 00000000000..4b4451d3e63 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME +fun main(args: Array) { + val t: String? = "abc" + if (t != null) t else throw NullPointerException("'t' must not be null") +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt.after b/idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt.after new file mode 100644 index 00000000000..63c53d875a2 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/throwsNPEwithArgument.kt.after @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun main(args: Array) { + "abc" ?: throw NullPointerException("'t' must not be null") +}