From b5f73ebd0f0b9ae385fa41c302e375b5190e0f23 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 10 Sep 2018 13:13:57 +0900 Subject: [PATCH] if-then to safe access: report even if condition in parentheses #KT-26662 Fixed --- .../intentions/branchedTransformations/IfThenUtils.kt | 4 ++-- .../ifThenToSafeAccess/conditionInParentheses.kt | 9 +++++++++ .../ifThenToSafeAccess/conditionInParentheses.kt.after | 7 +++++++ .../idea/inspections/LocalInspectionTestGenerated.java | 5 +++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index 5b2b068c404..135e26ace7d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -277,10 +277,10 @@ data class IfThenToSelectData( internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData? { val context = analyze() - val condition = condition as? KtOperationExpression ?: return null + val condition = condition?.unwrapBlockOrParenthesis() as? KtOperationExpression ?: return null val thenClause = then?.unwrapBlockOrParenthesis() val elseClause = `else`?.unwrapBlockOrParenthesis() - val receiverExpression = condition.checkedExpression() ?: return null + val receiverExpression = condition.checkedExpression()?.unwrapBlockOrParenthesis() ?: return null val (baseClause, negatedClause) = when (condition) { is KtBinaryExpression -> when (condition.operationToken) { diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt new file mode 100644 index 00000000000..55b36d7a849 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt @@ -0,0 +1,9 @@ +class Some { + fun bar() {} +} + +fun Some?.foo() { + if (((this) != null)) { + bar() + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt.after new file mode 100644 index 00000000000..9a72737fa00 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt.after @@ -0,0 +1,7 @@ +class Some { + fun bar() {} +} + +fun Some?.foo() { + this?.bar() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 0f3251f7517..782e2a5aafd 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -106,6 +106,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionComparesNullWithNull.kt"); } + @TestMetadata("conditionInParentheses.kt") + public void testConditionInParentheses() throws Exception { + runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt"); + } + @TestMetadata("conditionInvalidBinaryExp.kt") public void testConditionInvalidBinaryExp() throws Exception { runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInvalidBinaryExp.kt");