Add detekt for Kotlin static analysis#378
Open
holodorum wants to merge 4 commits intokson-org:mainfrom
Open
Conversation
Wires detekt 1.23.8 into the Gradle build, applied to every module that uses the Kotlin multiplatform or JVM plugin. Configuration is authored in detekt.kson and transpiled to detekt.yml via the existing TranspileKsonToYaml task, mirroring how .circleci/config.kson is handled. Pure style rules (MaxLineLength, WildcardImport, NewLineAtEndOfFile, PackageNaming) are disabled to keep the focus on code-quality findings; remaining findings are addressed in the follow-up commit.
Applies fixes for the actionable findings surfaced by the detekt setup
from the preceding commit, then tunes the config and baselines the
remaining structural debt so `./gradlew detekt` is green across every
module.
Code fixes:
- Replace unused `for (i in 1..n)` counters with `repeat(n)` in
Formatter.kt
- Replace `throw IllegalArgumentException(...)` guards with
`require(...)` / `requireNotNull(...)` in Message.kt and the schema
ref test
- Replace "should not happen" `RuntimeException` throws with the
existing `ShouldNotHappenException` across the parser, the
IntelliJ plugin highlighter / element type mapper, and LexerTest
- Convert `KsonIcons` to an `object` (it was a utility class with
a public constructor)
- Remove the empty body of the `KsonStringValidatorTest` placeholder
class; convert the constant-returning `searchExpressionSchema()`
test helper to a `val`
- Suppress `SwallowedException` at two intentional catch sites where
the exception type itself carries the full semantic
- Suppress `UnusedParameter` on the no-op native-test `validateYaml`
stub, which must match the JVM signature
Config changes in `detekt.kson`:
- Raise NestedBlockDepth to 6 and ComplexCondition to 10 to cover the
existing parser/validator code without being pathological
- Disable MatchingDeclarationName and TooGenericExceptionCaught — the
former would force a wave of file renames; the latter collides with
CLI commands that intentionally catch broadly to render
user-facing errors
Baselines:
- Per-module `detekt-baseline.xml` files capture the remaining
LongMethod / LargeClass / CyclomaticComplexMethod / ThrowsCount /
ReturnCount / LoopWithTooManyJumpStatements / SpreadOperator
findings on existing code. New code is still scrutinized against
every rule.
detekt.yml is transpiled from detekt.kson by transpileDetektConfigTask on every build, so it should not live in version control. Adds the file to .gitignore and removes the tracked copy from the index.
Adds a short section to the developer setup explaining what detekt does, that detekt.kson is the source of truth (detekt.yml is generated), and what the detekt-baseline.xml files carry: grandfathered pre-existing findings so detekt only fails the build on new issues. Also documents the "run detekt green, then detektBaseline, then diff" flow for safely refreshing the baselines after paying down debt.
Collaborator
|
Interesting! Will CI fail when there are unaddressed detekt warnings? It sounds like something we might want. |
Collaborator
Author
|
Yes, it should run with the |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Introduces detekt across all Kotlin modules so new code is held to a consistent set of static-analysis rules.
How it's wired up
detekt.ksonand transpiled todetekt.ymlat build time. The generated.ymlis gitignored — edit the.ksonsource of truth.detekt-baseline.xmlthat grandfathers pre-existing findings. Detekt ignores these but still fails the build on anything new, so pre-existing debt can be paid down incrementally without blocking the rollout.Refreshing baselines
Documented in the readme. Short version:
Running locally