Skip to content

Commit

Permalink
Add helper to check if a KtProperty is an "actual", and ignore said p…
Browse files Browse the repository at this point in the history
…roperties when checking "MayBeConst" rule. Fixes #4330 (#4331)
  • Loading branch information
gtcompscientist committed Nov 29, 2021
1 parent bfb049b commit 158c195
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class MayBeConst(config: Config = Config.empty) : Rule(config) {

private val topLevelConstants = HashSet<String?>()
private val companionObjectConstants = HashSet<String?>()
private val KtProperty.isActual
get() = hasModifier(KtTokens.ACTUAL_KEYWORD)

override fun visitKtFile(file: KtFile) {
topLevelConstants.clear()
Expand Down Expand Up @@ -110,6 +112,7 @@ class MayBeConst(config: Config = Config.empty) : Rule(config) {
private fun KtProperty.cannotBeConstant(): Boolean {
return isLocal ||
isVar ||
isActual ||
getter != null ||
isConstant() ||
isOverride()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down Expand Up @@ -305,6 +306,12 @@ class MayBeConstSpec : Spek({

assertThat(subject.findings).isEmpty()
}

it("does not report actual vals") {
// Until the [KotlinScriptEngine] can compile KMP, we will only lint.
subject.lint("""actual val abc123 = "abc123" """)
assertThat(subject.findings).isEmpty()
}
}

context("some const val candidates in nested objects") {
Expand Down

0 comments on commit 158c195

Please sign in to comment.