From c95ed9fd206a5770994dcc92509c2e48a761cee0 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 22 May 2018 17:13:48 +0300 Subject: [PATCH] Result unused: a few enhancements for PSI checks Related to KT-15063, KT-24433 --- .../kotlin/idea/inspections/AbstractResultUnusedChecker.kt | 7 +++++-- .../inspections/coroutines/asyncResultUnused/simple.kt | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractResultUnusedChecker.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractResultUnusedChecker.kt index 115e799ea0b..b03bfb074f1 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractResultUnusedChecker.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractResultUnusedChecker.kt @@ -21,11 +21,14 @@ abstract class AbstractResultUnusedChecker( protected fun check(expression: KtExpression): Boolean { // Check whatever possible by PSI if (!expressionChecker(expression, this)) return false + var current: PsiElement? = expression var parent: PsiElement? = expression.parent while (parent != null) { if (parent is KtBlockExpression || parent is KtFunction || parent is KtFile) break - if (parent is KtValueArgument) return false - if (parent is KtBinaryExpression && parent.operationToken in KtTokens.ALL_ASSIGNMENTS) return false + if (parent is KtValueArgument || parent is KtBinaryExpression || parent is KtUnaryExpression) return false + if (parent is KtQualifiedExpression && parent.receiverExpression == current) return false + // TODO: add when condition, if condition (later when it's applicable not only to Deferred) + current = parent parent = parent.parent } // Then check by call diff --git a/idea/testData/inspections/coroutines/asyncResultUnused/simple.kt b/idea/testData/inspections/coroutines/asyncResultUnused/simple.kt index 1a86eedf15e..fb94b6f127d 100644 --- a/idea/testData/inspections/coroutines/asyncResultUnused/simple.kt +++ b/idea/testData/inspections/coroutines/asyncResultUnused/simple.kt @@ -52,3 +52,10 @@ fun DbHandler.test() { doStuff().await() } +operator fun Deferred.unaryPlus() = this +operator fun Deferred.plus(arg: Int) = this + +fun moreFalsePositives() { + +(async { 0 }) + async { -1 } + 1 +}