Skip to content

Commit

Permalink
Redundant qualifier name: don't report for EnumClass.values/EnumClass…
Browse files Browse the repository at this point in the history
….valueOf

#KT-34696 Fixed
  • Loading branch information
t-kameyama authored and vladimirdolzhenko committed Apr 16, 2020
1 parent 611c4fe commit f747f85
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.compareDescriptors
import org.jetbrains.kotlin.idea.imports.importableFqName
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.util.hasNotReceiver
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
Expand All @@ -31,6 +31,10 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
import org.jetbrains.kotlin.resolve.scopes.utils.findFirstClassifierWithDeprecationStatus

class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
companion object {
private val ENUM_STATIC_METHODS = listOf("values", "valueOf")
}

override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor =
object : KtVisitorVoid() {
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
Expand All @@ -44,12 +48,16 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
else
expressionForAnalyze

val receiverReference = expressionForAnalyze.receiverExpression.mainReference?.resolve()
val parentEnumEntry = expressionForAnalyze.getStrictParentOfType<KtEnumEntry>()
if (parentEnumEntry != null) {
val companionObject = (expressionForAnalyze.receiverExpression.mainReference?.resolve() as? KtObjectDeclaration)
?.takeIf { it.isCompanion() }
val companionObject = (receiverReference as? KtObjectDeclaration)?.takeIf { it.isCompanion() }
if (companionObject?.containingClass() == parentEnumEntry.getStrictParentOfType<KtClass>()) return
}
if ((receiverReference as? KtClass)?.isEnum() == true
&& expressionForAnalyze.getParentOfTypesAndPredicate(true, KtClass::class.java) { it.isEnum() } != receiverReference
&& expressionForAnalyze.isEnumStaticMethodCall()
) return

val context = originalExpression.analyze()

Expand Down Expand Up @@ -77,6 +85,8 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
reportProblem(holder, applicableExpression)
}
}

private fun KtDotQualifiedExpression.isEnumStaticMethodCall() = callExpression?.calleeExpression?.text in ENUM_STATIC_METHODS
}

private tailrec fun KtDotQualifiedExpression.firstExpressionWithoutReceiver(): KtDotQualifiedExpression? = if (hasNotReceiver())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// PROBLEM: none
// WITH_RUNTIME
package packageName

import packageName.MyEnum.*

enum class MyEnum {
A, B, C, D, E;
}

fun main() {
<caret>MyEnum.valueOf("A")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// PROBLEM: none
// WITH_RUNTIME
package packageName

import packageName.MyEnum.*

enum class MyEnum {
A, B, C, D, E;
}

fun main() {
for (value in <caret>MyEnum.values()) {
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f747f85

Please sign in to comment.