generated from detekt/detekt-custom-rule-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Doist/piotr/no_not_null_operator
NoNotNullOperator rule
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ DoistRuleSet: | |
active: true | ||
MutableObservablePropertyIsPrivate: | ||
active: true | ||
NoNotNullOperator: | ||
active: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |