[kotlin] Temporarily exclude AbstractCollection.isEmpty and AbstractMap.isEmpty from UsePropertyAccessSyntaxInspection

KTIJ-31157 Temporarily exclude `isEmpty` from properties that UsePropertyAccessSyntaxInspection processes


(cherry picked from commit ecf69f310ffc8dce6b585f13ed88deb0b57a9321)

IJ-CR-146866

GitOrigin-RevId: 8b3c460bd2df54d219f695070a9bf3066309d0d9
This commit is contained in:
Victoria.Petrakovich
2024-10-15 19:09:42 +02:00
committed by intellij-monorepo-bot
parent c91c8b6702
commit eea506d923
6 changed files with 45 additions and 5 deletions

View File

@@ -28,6 +28,9 @@ interface NotPropertiesService {
for (byteBufferMethod in listOf("getChar", "getDouble", "getFloat", "getInt", "getLong", "getShort")) {
add("java.nio.ByteBuffer.$byteBufferMethod")
}
add("java.util.AbstractCollection.isEmpty") // KTIJ-31157
add("java.util.AbstractMap.isEmpty") // KTIJ-31157
}
fun getNotProperties(element: PsiElement): Set<FqNameUnsafe> {

View File

@@ -402,8 +402,8 @@ class UsePropertyAccessSyntaxInspection : LocalInspectionTool(), CleanupLocalIns
propertyName: String
): Boolean {
val allOverriddenSymbols = symbol.allOverriddenSymbolsWithSelf.toList()
if (functionOrItsAncestorIsInNotPropertiesList(allOverriddenSymbols, callExpression)) return false
if (functionOriginateNotFromJava(allOverriddenSymbols)) return false
if (functionNameIsInNotPropertiesList(symbol, callExpression)) return false
// Check that the receiver or its ancestors don't have public fields with the same name as the probable synthetic property
if (receiverOrItsAncestorsContainVisibleFieldWithSameName(receiverType, propertyName)) return false
@@ -554,11 +554,18 @@ class UsePropertyAccessSyntaxInspection : LocalInspectionTool(), CleanupLocalIns
return replacementReceiverType.semanticallyEquals(expectedReceiverType)
}
private fun functionNameIsInNotPropertiesList(symbol: KaCallableSymbol, callExpression: KtExpression): Boolean {
val symbolUnsafeName = symbol.callableId?.asSingleFqName()?.toUnsafe()
private fun functionOrItsAncestorIsInNotPropertiesList(
allOverriddenSymbols: List<KaCallableSymbol>,
callExpression: KtExpression
): Boolean {
val notProperties = NotPropertiesService.getNotProperties(callExpression)
return symbolUnsafeName in notProperties
for (overriddenSymbol in allOverriddenSymbols) {
val symbolUnsafeName = overriddenSymbol.callableId?.asSingleFqName()?.toUnsafe()
?: return true // something went wrong, and it's better to stop the inspection
if (symbolUnsafeName in notProperties) return true
}
return false
}
context(KaSession)

View File

@@ -6380,6 +6380,16 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp
runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIfPropertyHasLambdaAndSetterInsideIsLast.kt");
}
@TestMetadata("dontReplaceIsEmptyOfAbstractCollectionInheritor.kt")
public void testDontReplaceIsEmptyOfAbstractCollectionInheritor() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIsEmptyOfAbstractCollectionInheritor.kt");
}
@TestMetadata("dontReplaceIsEmptyOfAbstractMapInheritor.kt")
public void testDontReplaceIsEmptyOfAbstractMapInheritor() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIsEmptyOfAbstractMapInheritor.kt");
}
@TestMetadata("dontReplaceIsGetterWithNonBooleanReturnType.kt")
public void testDontReplaceIsGetterWithNonBooleanReturnType() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIsGetterWithNonBooleanReturnType.kt");

View File

@@ -19302,6 +19302,16 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIfPropertyHasLambdaAndSetterInsideIsLast.kt");
}
@TestMetadata("dontReplaceIsEmptyOfAbstractCollectionInheritor.kt")
public void testDontReplaceIsEmptyOfAbstractCollectionInheritor() throws Exception {
runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIsEmptyOfAbstractCollectionInheritor.kt");
}
@TestMetadata("dontReplaceIsEmptyOfAbstractMapInheritor.kt")
public void testDontReplaceIsEmptyOfAbstractMapInheritor() throws Exception {
runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIsEmptyOfAbstractMapInheritor.kt");
}
@TestMetadata("dontReplaceIsGetterWithNonBooleanReturnType.kt")
public void testDontReplaceIsGetterWithNonBooleanReturnType() throws Exception {
runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceIsGetterWithNonBooleanReturnType.kt");

View File

@@ -0,0 +1,5 @@
// PROBLEM: none
fun test() {
java.util.HashSet<Int>().<caret>isEmpty()
}

View File

@@ -0,0 +1,5 @@
// PROBLEM: none
fun test() {
java.util.HashMap<String, Int>().<caret>isEmpty()
}