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

#5014 Fix MaxChainedCallsOnSameLine false positives #5020

Merged
merged 1 commit into from Jul 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,6 +10,8 @@ import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtUnaryExpression

Expand Down Expand Up @@ -39,10 +41,13 @@ class MaxChainedCallsOnSameLine(config: Config = Config.empty) : Rule(config) {
override fun visitQualifiedExpression(expression: KtQualifiedExpression) {
super.visitQualifiedExpression(expression)

// skip if the parent is also a call on the same line to avoid duplicated warnings
val parent = expression.parent

// skip if the parent is also a call on the same line to avoid duplicated warnings
if (parent is KtQualifiedExpression && !parent.callOnNewLine()) return

if (parent is KtImportDirective || parent is KtPackageDirective) return

val chainedCalls = expression.countChainedCalls() + 1
if (chainedCalls > maxChainedCalls) {
report(
Expand Down
Expand Up @@ -3,6 +3,7 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.junit.jupiter.api.Test

class MaxChainedCallsOnSameLineSpec {
Expand Down Expand Up @@ -104,4 +105,20 @@ class MaxChainedCallsOnSameLineSpec {

assertThat(rule.compileAndLint(code)).hasSize(1)
}

@Test
fun `does not report long imports`() {
val rule = MaxChainedCallsOnSameLine(TestConfig(mapOf("maxChainedCalls" to 3)))
val code = "import a.b.c.d.e"

assertThat(rule.lint(code)).isEmpty()
}

@Test
fun `does not report long package declarations`() {
val rule = MaxChainedCallsOnSameLine(TestConfig(mapOf("maxChainedCalls" to 3)))
val code = "package a.b.c.d.e"

assertThat(rule.lint(code)).isEmpty()
}
}