8000 Improve UnnecessaryApply by BraisGabin · Pull Request #2827 · detekt/detekt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve UnnecessaryApply #2827

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

Merged
merged 3 commits into from
Jun 27, 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
10000
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
import org.jetbrains.kotlin.psi.KtThisExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
Expand All @@ -27,6 +28,7 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
* <noncompliant>
* config.apply { version = "1.2" } // can be replaced with `config.version = "1.2"`
* config?.apply { environment = "test" } // can be replaced with `config?.environment = "test"`
* config?.apply { println(version) } // `apply` can be replaced by `let`
* </noncompliant>
*
* <compliant>
Expand All @@ -47,10 +49,12 @@ class UnnecessaryApply(config: Config) : Rule(config) {
if (expression.isApplyExpr() &&
expression.hasOnlyOneMemberAccessStatement() &&
expression.receiverIsUnused(bindingContext)) {
report(CodeSmell(
issue, Entity.from(expression),
"apply expression can be omitted"
))
val message = if (expression.parent is KtSafeQualifiedExpression) {
"apply can be replaced with let or an if"
} else {
"apply expression can be omitted"
}
report(CodeSmell(issue, Entity.from(expression), message))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,31 @@ class UnnecessaryApplySpec : Spek({
context("unnecessary apply expressions that can be changed to ordinary method call") {

it("reports an apply on non-nullable type") {
assertThat(subject.compileAndLint("""
val findings = subject.compileAndLint("""
fun f() {
val a : Int = 0
val a: Int = 0
a.apply {
plus(1)
}
}
""")).hasSize(1)
""")
assertThat(findings).hasSize(1)
assertThat(findings.first().message).isEqualTo("apply expression can be omitted")
}

it("reports an apply on nullable type") {
assertThat(subject.compileAndLint("""
val findings = subject.compileAndLint("""
fun f() {
val a : Int? = null
val a: Int? = null
// Resolution: we can't say here if plus is on 'this' or just a side effect when a is not null
// However such cases should be better handled with an if-null check instead of misusing apply
a?.apply {
plus(1)
}
}
""")).hasSize(1)
""")
assertThat(findings).hasSize(1)
assertThat(findings.first().message).isEqualTo("apply can be replaced with let or an if")
}

it("reports a false negative apply on nullable type - #1485") {
Expand Down Expand Up @@ -70,7 +74,7 @@ class UnnecessaryApplySpec : Spek({

it("does report single statement in apply used as function argument") {
assertThat(subject.compileAndLint("""
fun b(i : Int?) {}
fun b(i: Int?) {}

fun main() {
val a: Int? = null
Expand Down Expand Up @@ -105,10 +109,10 @@ class UnnecessaryApplySpec : Spek({

it("does not report applies with lambda body containing more than one statement") {
assertThat(subject.compileAndLint("""
fun b(i : Int?) {}
fun b(i: Int?) {}

fun main() {
val a : Int? = null
val a: Int? = null
a?.apply {
plus(1)
plus(2)
Expand Down
1 change: 1 addition & 0 deletions docs/pages/documentation/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ an ordinary method/extension function call to reduce visual complexity
```kotlin
config.apply { version = "1.2" } // can be replaced with `config.version = "1.2"`
config?.apply { environment = "test" } // can be replaced with `config?.environment = "test"`
config?.apply { println(version) } // `apply` can be replaced by `let`
```

#### Compliant Code:
Expand Down
0