Skip to content

Commit

Permalink
ExplicitCollectionElementAccessMethod: fix false positive for get ope…
Browse files Browse the repository at this point in the history
…rators with type parameters (#4803)
  • Loading branch information
t-kameyama committed May 21, 2022
1 parent fe27504 commit 5f491f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Expand Up @@ -57,8 +57,11 @@ class ExplicitCollectionElementAccessMethod(config: Config = Config.empty) : Rul
}
}

private fun isIndexableGetter(expression: KtCallExpression): Boolean =
expression.calleeExpression?.text == "get" && expression.getFunctionDescriptor()?.isOperator == true
private fun isIndexableGetter(expression: KtCallExpression): Boolean {
if (expression.calleeExpression?.text != "get") return false
val descriptor = expression.getFunctionDescriptor() ?: return false
return descriptor.isOperator && descriptor.typeParameters.isEmpty()
}

private fun isIndexableSetter(expression: KtCallExpression): Boolean =
when (expression.calleeExpression?.text) {
Expand Down
Expand Up @@ -347,6 +347,19 @@ class ExplicitCollectionElementAccessMethodSpec(val env: KotlinCoreEnvironment)
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `does not report custom get operator with type parameters`() {
val code = """
class C {
operator fun <T> get(key: String): List<T>? = null
}
fun test(c: C) {
c.get<Int>("key")
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}

@Nested
Expand Down

0 comments on commit 5f491f1

Please sign in to comment.