ReplaceGetOrSetInspection: do not report for 'get' with spread operator
#KT-36171 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b782e8f0f0
commit
0c9720f20c
+5
-6
@@ -45,6 +45,11 @@ class ReplaceGetOrSetInspection : AbstractApplicabilityBasedInspection<KtDotQual
|
||||
val callExpression = element.callExpression ?: return false
|
||||
val calleeName = (callExpression.calleeExpression as? KtSimpleNameExpression)?.getReferencedNameAsName()
|
||||
if (calleeName !in operatorNames) return false
|
||||
if (callExpression.typeArgumentList != null) return false
|
||||
val arguments = callExpression.valueArguments
|
||||
if (arguments.isEmpty()) return false
|
||||
if (arguments.any { it.isNamed() || it.isSpread }) return false
|
||||
|
||||
val bindingContext = callExpression.analyze(BodyResolveMode.PARTIAL_WITH_CFA)
|
||||
val resolvedCall = callExpression.getResolvedCall(bindingContext) ?: return false
|
||||
if (!resolvedCall.isReallySuccess()) return false
|
||||
@@ -52,12 +57,6 @@ class ReplaceGetOrSetInspection : AbstractApplicabilityBasedInspection<KtDotQual
|
||||
val target = resolvedCall.resultingDescriptor as? FunctionDescriptor ?: return false
|
||||
if (!target.isValidOperator() || target.name !in operatorNames) return false
|
||||
|
||||
if (callExpression.typeArgumentList != null) return false
|
||||
|
||||
val arguments = callExpression.valueArguments
|
||||
if (arguments.isEmpty()) return false
|
||||
if (arguments.any { it.isNamed() }) return false
|
||||
|
||||
if (!element.isReceiverExpressionWithValue()) return false
|
||||
|
||||
return target.name != OperatorNameConventions.SET || !element.isUsedAsExpression(bindingContext)
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// PROBLEM: none
|
||||
class A {
|
||||
operator fun get(vararg args: Any) {
|
||||
println(args.size)
|
||||
}
|
||||
|
||||
private fun println(i: Int) {}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val args = arrayOf("a", 1)
|
||||
A().<caret>get(*args)
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// PROBLEM: none
|
||||
class B {
|
||||
operator fun get(i: Int, vararg args: String) {
|
||||
println(args.size)
|
||||
}
|
||||
|
||||
private fun println(i: Int) {}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val args = arrayOf("a", "b")
|
||||
B().<caret>get(1, *args)
|
||||
}
|
||||
+10
@@ -3142,6 +3142,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceGetOrSet/functionalArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("getWithSpread.kt")
|
||||
public void testGetWithSpread() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceGetOrSet/getWithSpread.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("getWithSpread2.kt")
|
||||
public void testGetWithSpread2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceGetOrSet/getWithSpread2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("invalidArgument.kt")
|
||||
public void testInvalidArgument() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceGetOrSet/invalidArgument.kt");
|
||||
|
||||
Reference in New Issue
Block a user