UnusedUnaryOperatorInspection: shouldn't report on annotation entry
#KT-37294 Fixed
This commit is contained in:
@@ -15,8 +15,11 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.util.textRangeIn
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtPrefixExpression
|
||||
import org.jetbrains.kotlin.psi.prefixExpressionVisitor
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
@@ -27,6 +30,8 @@ class UnusedUnaryOperatorInspection : AbstractKotlinInspection() {
|
||||
val operationToken = prefix.operationToken
|
||||
if (operationToken != KtTokens.PLUS && operationToken != KtTokens.MINUS) return
|
||||
|
||||
// ISSUE: KT-37447
|
||||
if (prefix.isInAnnotationEntry) return
|
||||
val context = prefix.analyze(BodyResolveMode.PARTIAL_WITH_CFA)
|
||||
if (prefix.isUsedAsExpression(context)) return
|
||||
val operatorDescriptor = prefix.operationReference.getResolvedCall(context)?.resultingDescriptor as? DeclarationDescriptor ?: return
|
||||
@@ -53,3 +58,6 @@ class UnusedUnaryOperatorInspection : AbstractKotlinInspection() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val KtPrefixExpression.isInAnnotationEntry: Boolean
|
||||
get() = parentsWithSelf.takeWhile { it is KtExpression }.last().parent?.parent?.parent is KtAnnotationEntry
|
||||
@@ -0,0 +1,6 @@
|
||||
// PROBLEM: none
|
||||
|
||||
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||
annotation class Range(val min: Long = 0)
|
||||
|
||||
fun foo(@Range(min = -<caret>90L) x: Int) {}
|
||||
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
annotation class Range(val min: Long = 0)
|
||||
|
||||
@Range(min = -<caret>90L)
|
||||
fun foo(x: Int) {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// PROBLEM: none
|
||||
|
||||
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||
annotation class Range(val min: Long = 0)
|
||||
|
||||
fun foo(@Range(min = -(-<caret>90L)) x: Int) {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun a(a: Int) = Unit
|
||||
fun b() {
|
||||
a(a = -<caret>1)
|
||||
}
|
||||
+20
@@ -13308,6 +13308,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/unusedUnaryOperator"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("annotation.kt")
|
||||
public void testAnnotation() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/annotation.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("annotation2.kt")
|
||||
public void testAnnotation2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/annotation2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("annotation3.kt")
|
||||
public void testAnnotation3() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/annotation3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("basic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/basic.kt");
|
||||
@@ -13318,6 +13333,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/basic2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("namedParameter.kt")
|
||||
public void testNamedParameter() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/namedParameter.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("usedAsExpression.kt")
|
||||
public void testUsedAsExpression() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/unusedUnaryOperator/usedAsExpression.kt");
|
||||
|
||||
Reference in New Issue
Block a user