From 3615c20f1bd3c5bb80de88ce5aa209025436ecfd Mon Sep 17 00:00:00 2001 From: Marharyta Nedzelska Date: Tue, 26 Nov 2024 12:01:51 +0100 Subject: [PATCH] Simplify 'isK2' check --- .../kotlin/checks/UnnecessaryImportsCheck.kt | 23 ++++----------- .../checks/UnnecessaryImportsHelper.java | 29 ------------------- 2 files changed, 6 insertions(+), 46 deletions(-) delete mode 100644 sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsHelper.java diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsCheck.kt b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsCheck.kt index 57c5965a9..27a275007 100644 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsCheck.kt +++ b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsCheck.kt @@ -20,26 +20,13 @@ package org.sonarsource.kotlin.checks import org.jetbrains.kotlin.analysis.api.KaIdeApi +import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.descriptors.VariableAccessorDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors import org.jetbrains.kotlin.descriptors.accessors import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.psi.KtArrayAccessExpression -import org.jetbrains.kotlin.psi.KtCallExpression -import org.jetbrains.kotlin.psi.KtElement -import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.psi.KtImportDirective -import org.jetbrains.kotlin.psi.KtImportList -import org.jetbrains.kotlin.psi.KtOperationReferenceExpression -import org.jetbrains.kotlin.psi.KtPackageDirective -import org.jetbrains.kotlin.psi.KtPrefixExpression -import org.jetbrains.kotlin.psi.KtProperty -import org.jetbrains.kotlin.psi.KtPropertyDelegate -import org.jetbrains.kotlin.psi.KtReferenceExpression -import org.jetbrains.kotlin.psi.KtSimpleNameExpression -import org.jetbrains.kotlin.psi.KtTreeVisitorVoid -import org.jetbrains.kotlin.psi.KtUserType +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.psi2ir.deparenthesize @@ -53,7 +40,6 @@ import org.sonar.check.Rule import org.sonarsource.kotlin.api.checks.AbstractCheck import org.sonarsource.kotlin.api.frontend.KotlinFileContext import org.sonarsource.kotlin.api.visiting.analyze -import org.sonarsource.kotlin.checks.UnnecessaryImportsHelper.isK2 private const val MESSAGE_UNUSED = "Remove this unused import." private const val MESSAGE_REDUNDANT = "Remove this redundant import." @@ -68,7 +54,7 @@ class UnnecessaryImportsCheck : AbstractCheck() { override fun visitKtFile(file: KtFile, context: KotlinFileContext) { analyze { - if (isK2(this)) { + if (this.isK2()) { val analyzeImportsToOptimize = analyzeImportsToOptimize(file) file.importDirectives.mapNotNull { import -> import.importedFqName?.let { import to it } } @@ -285,3 +271,6 @@ private fun KtReferenceExpression.importableSimpleName() = is KtSimpleNameExpression -> getReferencedName() else -> null } + +fun KaSession.isK2(): Boolean = + "org.jetbrains.kotlin.analysis.api.fir.KaFirSession" == javaClass.name diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsHelper.java b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsHelper.java deleted file mode 100644 index 086e83d56..000000000 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/UnnecessaryImportsHelper.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SonarSource Kotlin - * Copyright (C) 2018-2024 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarsource.kotlin.checks; - -import org.jetbrains.kotlin.analysis.api.KaSession; -import org.jetbrains.kotlin.analysis.api.fir.KaFirSession; - -public class UnnecessaryImportsHelper { - public static boolean isK2(KaSession session) { - return session instanceof KaFirSession; - } -}