KTIJ-31590 K2: don't warn about unused parameters for external functions

GitOrigin-RevId: 3b010e9f20478f6d4b0d0fc60579ea91b42d300e
This commit is contained in:
Alexey Kudravtsev
2024-10-17 14:15:32 +00:00
committed by intellij-monorepo-bot
parent 0bfb7b64c3
commit cfa1938d3d
5 changed files with 23 additions and 4 deletions
@@ -307,6 +307,11 @@ public abstract class K2UnusedSymbolHighlightingTestGenerated extends AbstractK2
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/expectFunctionParameter.kt");
}
@TestMetadata("externalFunWithUnusedParameter.kt")
public void testExternalFunWithUnusedParameter() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/externalFunWithUnusedParameter.kt");
}
@TestMetadata("functionCall.kt")
public void testFunctionCall() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/unusedSymbol/functionCall.kt");
@@ -130,6 +130,9 @@ object K2UnusedSymbolUtil {
context(KaSession)
@OptIn(KaExperimentalApi::class)
fun getPsiToReportProblem(declaration: KtNamedDeclaration, isJavaEntryPointInspection: UnusedDeclarationInspectionBase): PsiElement? {
if (((declaration as? KtParameter)?.parent?.parent as? KtModifierListOwner)?.hasModifier(KtTokens.EXTERNAL_KEYWORD) == true) {
return null
}
val symbol = declaration.symbol
if (declaration.languageVersionSettings.getFlag(
AnalysisFlags.explicitApiMode) != ExplicitApiMode.DISABLED && symbol.compilerVisibility.isPublicAPI) {
@@ -647,7 +650,7 @@ object K2UnusedSymbolUtil {
return when {
symbol is KaConstructorSymbol -> {
val classSymbol = symbol.containingDeclaration as? KaNamedClassSymbol ?: return false
!classSymbol.isInline && !(classSymbol.visibility == KaSymbolVisibility.PRIVATE)
!classSymbol.isInline && classSymbol.visibility != KaSymbolVisibility.PRIVATE
}
hasModifier(KtTokens.INTERNAL_KEYWORD) -> false
symbol !is KaNamedFunctionSymbol -> true
@@ -161,9 +161,10 @@ class KotlinUnusedHighlightingVisitor(private val ktFile: KtFile) {
holder: HighlightInfoHolder) {
if (!K2UnusedSymbolUtil.isApplicableByPsi(declaration)) return
if (refHolder.isUsedLocally(declaration)) return // even for non-private declarations our refHolder might have usage info
val mustBeLocallyReferenced = declaration is KtParameter && !(declaration.hasValOrVar()) ||
declaration.hasModifier(KtTokens.PRIVATE_KEYWORD) ||
((declaration.parent as? KtClassBody)?.parent as? KtClassOrObject)?.isLocal == true
val mustBeLocallyReferenced = declaration is KtParameter && !declaration.hasValOrVar()
&& (declaration.parent?.parent as? KtModifierListOwner)?.hasModifier(KtTokens.EXTERNAL_KEYWORD) != true // parameters of external functions might be referenced elsewhere
|| declaration.hasModifier(KtTokens.PRIVATE_KEYWORD)
|| ((declaration.parent as? KtClassBody)?.parent as? KtClassOrObject)?.isLocal == true
if (SuppressionUtil.inspectionResultSuppressed(declaration, deadCodeInspection)) {
return
}
@@ -18173,6 +18173,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/unusedSymbol/expectFunctionParameter.kt");
}
@TestMetadata("externalFunWithUnusedParameter.kt")
public void testExternalFunWithUnusedParameter() throws Exception {
runTest("testData/inspectionsLocal/unusedSymbol/externalFunWithUnusedParameter.kt");
}
@TestMetadata("functionCall.kt")
public void testFunctionCall() throws Exception {
runTest("testData/inspectionsLocal/unusedSymbol/functionCall.kt");
@@ -0,0 +1,5 @@
// PROBLEM: none
class M {
private external fun sdkfjlsdkjf(<caret>sdfsd: Int):Int
override fun hashCode() = sdkfjlsdkjf(0)
}