-
Notifications
You must be signed in to change notification settings - Fork 1
ANDR-103: добавила правило detekt #152
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
Open
Nuriua
wants to merge
1
commit into
develop
Choose a base branch
from
feature/ANDR-103
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 @@ | ||
| /build |
This file contains hidden or 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,19 @@ | ||
| plugins { | ||
| id("java-library") | ||
| alias(libs.plugins.jvm) | ||
| } | ||
| java { | ||
| sourceCompatibility = JavaVersion.VERSION_11 | ||
| targetCompatibility = JavaVersion.VERSION_11 | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.detekt.api) | ||
| compileOnly(libs.kotlin.compiler.embeddable) | ||
| } | ||
|
|
||
| kotlin { | ||
| compilerOptions { | ||
| jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11 | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
detekt-rules/src/main/kotlin/ru/yeahub/rules/CustomRuleSetProvider.kt
This file contains hidden or 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,17 @@ | ||
| package ru.yeahub.rules | ||
|
|
||
| import io.gitlab.arturbosch.detekt.api.Config | ||
| import io.gitlab.arturbosch.detekt.api.RuleSet | ||
| import io.gitlab.arturbosch.detekt.api.RuleSetProvider | ||
|
|
||
| class CustomRuleSetProvider : RuleSetProvider { | ||
|
|
||
| override val ruleSetId: String = "yeahub" | ||
|
|
||
| override fun instance(config: Config): RuleSet { | ||
| return RuleSet( | ||
| id = ruleSetId, | ||
| rules = listOf(NoNotNullAssertionRule(config)) | ||
| ) | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
detekt-rules/src/main/kotlin/ru/yeahub/rules/NoNotNullAssertionRule.kt
This file contains hidden or 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,38 @@ | ||
| package ru.yeahub.rules | ||
|
|
||
| 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.KtUnaryExpression | ||
|
|
||
| class NoNotNullAssertionRule(config: Config = Config.Companion.empty) : Rule(config) { | ||
|
|
||
| init { | ||
| println(">>> NoNotNullAssertionRule: ПРАВИЛО ЗАГРУЖЕНО! <<<") | ||
| } | ||
|
|
||
| override val issue = Issue( | ||
| "NoNotNullAssertionRule", | ||
| Severity.Style, | ||
| "Оператор !! запрещён. Используйте безопасные вызовы (.?) или элвис-оператор (?:).", | ||
| Debt.Companion.FIVE_MINS | ||
| ) | ||
|
|
||
| override fun visitUnaryExpression(expression: KtUnaryExpression) { | ||
| val operatorText = expression.operationReference.text | ||
| if (operatorText == "!!") { | ||
| report( | ||
| CodeSmell( | ||
| issue, | ||
| Entity.Companion.from(expression), | ||
| message = "Использование оператора !! запрещено" | ||
| ) | ||
| ) | ||
| } | ||
| super.visitUnaryExpression(expression) | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...ules/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider
This file contains hidden or 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 @@ | ||
| ru.yeahub.rules.CustomRuleSetProvider |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Предлагаю расширить формулировку - замена !! на ?. или ?: не всегда корректна, потому что в местах с обязательным контрактом это может скрыть ошибку дефолтным значением или невыполнением кода. Лучше явно разрешить fail-fast варианты: requireNotNull для проверки входных аргументов и checkNotNull для проверки внутренних инвариантов/состояния. Тогда мы не теряем понятное падение там, где null означает баг в коде.
(понятно, что текущая подсказка их не запрещает - но может сложиться впечатление что разрешены только ?: и .?)
Или добавить подсказку, что для явных fail-fast вариантов можно использовать ?: error("error text") - мне было не очевидно что ?: возможно применять и таким образом.