Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForbiddenMethodCall: report operator calls #3032

Merged
merged 1 commit into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.valueOrDefaultCommaSeparated
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtPostfixExpression
import org.jetbrains.kotlin.psi.KtPrefixExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
Expand Down Expand Up @@ -44,6 +48,25 @@ class ForbiddenMethodCall(config: Config = Config.empty) : Rule(config) {

override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
check(expression)
}

override fun visitBinaryExpression(expression: KtBinaryExpression) {
super.visitBinaryExpression(expression)
check(expression.operationReference)
}

override fun visitPrefixExpression(expression: KtPrefixExpression) {
super.visitPrefixExpression(expression)
check(expression.operationReference)
}

override fun visitPostfixExpression(expression: KtPostfixExpression) {
super.visitPostfixExpression(expression)
check(expression.operationReference)
}

private fun check(expression: KtExpression) {
if (bindingContext == BindingContext.EMPTY) return

val resolvedCall = expression.getResolvedCall(bindingContext) ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,43 @@ class ForbiddenMethodCallSpec : Spek({
assertThat(findings).hasSize(2)
assertThat(findings).hasTextLocations(48 to 64, 76 to 80)
}

it("should report equals operator") {
val code = """
fun main() {
java.math.BigDecimal(5.5) == java.math.BigDecimal(5.5)
}
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("java.math.BigDecimal.equals")))
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}

it("should report prefix operator") {
val code = """
fun test() {
var i = 1
++i
}
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("kotlin.Int.inc")))
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}

it("should report postfix operator") {
val code = """
fun test() {
var i = 1
i--
}
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("kotlin.Int.dec")))
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
}
})