Skip to content

Commit

Permalink
Merge pull request #6 from Doist/piotr/no_not_null_operator
Browse files Browse the repository at this point in the history
NoNotNullOperator rule
  • Loading branch information
panpeter authored Jul 14, 2022
2 parents 61b459f + 9bf7b44 commit 5d0ccf9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ val a = when {
### MutableObservablePropertyIsPrivate

This rule reports exposed `MutableLive...` and `MutableStateFlow` properties. They should be
private.
private.

### NoNotNullOperator

This rule reports `!!` usage. `requireNotNull` should be used instead.
32 changes: 32 additions & 0 deletions src/main/kotlin/com/doist/detekt/NoNotNullOperator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.doist.detekt

import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import org.jetbrains.kotlin.psi.KtPostfixExpression

class NoNotNullOperator(config: Config) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Style,
"Detects !! operator",
Debt.FIVE_MINS,
)

override fun visitPostfixExpression(expression: KtPostfixExpression) {
super.visitPostfixExpression(expression)

if (expression.text.endsWith("!!")) {
val codeSmell = CodeSmell(
issue = issue,
entity = Entity.from(expression),
message = "Use requireNotNull instead of !! operator",
)
report(codeSmell)
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ DoistRuleSet:
active: true
MutableObservablePropertyIsPrivate:
active: true
NoNotNullOperator:
active: true
33 changes: 33 additions & 0 deletions src/test/kotlin/com/doist/detekt/NoNotNullOperatorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.doist.detekt

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.kotest.matchers.collections.shouldHaveSize
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test

@KotlinCoreEnvironmentTest
internal class NoNotNullOperatorTest(private val env: KotlinCoreEnvironment) {
@Test
fun `reports !! usage`() {
val code = """
val a: String? = ""
val b = a!!
"""
val rule = NoNotNullOperator(Config.empty)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}

@Test
fun `doesn't reports requireNoteNull usage`() {
val code = """
val a: String? = ""
val b = requireNotNull(a)
"""
val rule = NoNotNullOperator(Config.empty)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}
}

0 comments on commit 5d0ccf9

Please sign in to comment.