detekt
Meet detekt, a static code analysis tool for the Kotlin programming language. It operates on the abstract syntax tree provided by the Kotlin compiler.
Features
- Code smell analysis for your Kotlin projects
- Complexity reports based on lines of code, cyclomatic complexity and amount of code smells
- Highly configurable rule sets
- Suppression of findings with Kotlin's
@Suppress
and Java's@SuppressWarnings
annotations - Specification of quality gates which will break your build
- Code Smell baseline and suppression for legacy projects
- Gradle plugin for code analysis via Gradle builds
- SonarQube integration
- Extensibility by enabling incorporation of personal rule sets,
FileProcessListener's
andOutputReport's
- IntelliJ integration
- Third party integrations for Maven, Bazel and Github Actions (Docker based and Javascript based)
Project Website
Visit the project website for installation guides, release notes, migration guides, rule descriptions and configuration options.
Quick-Links
- Changelog and migration guides
- Available CLI options
- Rule set and rule descriptions
- Writing custom rules and extending detekt
- Suppressing issues in code
- Suppressing issues via baseline file
- Configuring detekt
- Sample Gradle integrations examples:
Quick Start ...
with the command-line interface
curl -sSLO https://github.com/detekt/detekt/releases/download/v[version]/detekt && chmod a+x detekt
./detekt --help
You can find other ways to install detekt here
with Gradle
Gradle 5.4+ is required:
buildscript {
repositories {
jcenter()
}
// or
mavenCentral()
jcenter {
content {
// Only download the 'kotlinx-html-jvm' module from JCenter, but nothing else.
// detekt needs 'kotlinx-html-jvm' for the HTML report.
includeModule("org.jetbrains.kotlinx", "kotlinx-html-jvm")
}
}
}
plugins {
id("io.gitlab.arturbosch.detekt").version("[version]")
}
detekt {
buildUponDefaultConfig = true // preconfigure defaults
allRules = false // activate all available (even unstable) rules.
config = files("$projectDir/config/detekt.yml") // point to your custom config defining rules to run, overwriting default behavior
baseline = file("$projectDir/config/baseline.xml") // a way of suppressing issues before introducing detekt
reports {
html.enabled = true // observe findings in your browser with structure and code snippets
xml.enabled = true // checkstyle like format mainly for integrations like Jenkins
txt.enabled = true // similar to the console output, contains issue signature to manually edit baseline files
sarif.enabled = true // standardized SARIF format (https://sarifweb.azurewebsites.net/) to support integrations with Github Code Scanning
}
}
// Groovy DSL
tasks.withType(Detekt).configureEach {
jvmTarget = "1.8"
}
// or
// Kotlin DSL
tasks.withType<Detekt>.configureEach {
// Target version of the generated JVM bytecode. It is used for type resolution.
jvmTarget = "1.8"
}
See maven central for releases and sonatype for snapshots.
If you want to use a SNAPSHOT version, you can find more info on this documentation page.
Adding more rule sets
detekt itself provides a wrapper over ktlint as a formatting
rule set
which can be easily added to the Gradle configuration:
dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:[version]")
}
Likewise custom extensions can be added to detekt.
Contributors
If you contributed to detekt but your name is not in the list, please feel free to add yourself to it!
- Artur Bosch - Maintainer
- Marvin Ramin - Collaborator, Bunch of rules, Active on Issues, refactorings, MultiRule
- schalks - Collaborator, Active on Issues, Bunch of rules, Project metrics
- Niklas Baudy - Active on Issues, Bunch of rules, Bug fixes
- lummax - Cli enhancements
- Svyatoslav Chatchenko - Active on Issues, NamingConventions and UnusedImport fixes
- Sean Flanigan - Config from classpath resource
- Sebastian Schuberth - Active on Issues, Windows support
- Olivier Lemasle - NP-Bugfix, rules fixes, Gradle plugin improvement
- Marc Prengemann - Support for custom output formats, prototyped Rule-Context-Issue separation
- Sebastiano Poggi - Build tooling improvements, rules improvements and fixes, docs fixes, Gradle plugin improvements
- Ilya Tretyakov - Sonar runs should not auto correct formatting.
- Andrey T - Readme fix
- Ivan Balaksha - Rules: UnsafeCast, SpreadOperator, UnsafeCallOnNullableType, LabeledExpression
- Anna Y - Readme fix
- Karol Wrótniak - Treat comments as not empty blocks
- Radim Vaculik - VariableMaxLength - bugfix
- Martin Nonnenmacher - UndocumentedPublicClass - enum support
- Dmytro Troynikov - Updated Magic Number rule to ignore Named Arguments
- Andrew Ochsner - Updated Readme for
failFast
option - Paul Merlin - Gradle build improvements
- Konstantin Aksenov - Coding improvement
- Matthew Haughton - Added type resolution, Dependency updates, Coding + Documentation improvements
- Janusz Bagiński - Fixed line number reporting for MaxLineLengthRule
- Mike Kobit - Gradle build improvements
- Philipp Hofmann - Readme improvements
- Olivier PEREZ - Fixed Typo in Readme
- Sebastian Kaspari - Html-Output-Format, Documentation fix
- Ilya Zorin - Rule improvement: UnnecessaryAbstractClass
- Gesh Markov - Improve error message for incorrect configuration file
- Patrick Pilch - Rule improvement: ReturnCount
- Serj Lotutovici - Rule improvement: LongParameterList
- Dmitry Primshyts - Rule improvement: MagicNumber
- Egor Neliuba - Rule improvement: EmptyFunctionBlock, EmptyClassBlock
- Said Tahsin Dane - Gradle plugin improvements
- Misa Torres - Added: TrailingWhitespace and NoTabs rules
- R.A. Porter - Updated Readme links to RuleSets
- Robbin Voortman - Rule improvement: MaxLineLength
- Mike Gorunov — Rule improvement: UndocumentedPublicFunction
- Joey Kaan - New rule: MandatoryBracesIfStatements
- Dmitriy Samaryan - Rule fix: SerialVersionUIDInSerializableClass
- Mariano Simone - Rule improvement: UnusedPrivateMember. New Rules: UnusedPrivateClass, VarCouldBeVal
- Shunsuke Maeda - Fix: to work on multi module project using maven plugin
- Mikhail Levchenko - New rules: Unnecessary let, ExplicitItLambdaParameter
- Scott Kennedy - Minor fixes
- Mickele Moriconi - Added: ConstructorParameterNaming and FunctionParameterNaming rules
- Lukasz Jazgar - Fixed configuring formatting rules
- Pavlos-Petros Tournaris - Lazy evaluation of Regex in Rules
- Erhard Pointl - Kotlin DSL and Gradle enhancements
- Tyler Thrailkill - FunctionNaming rule enhancements
- Tarek Belkahia - TooManyFunctions rule options
- Bournane Abdelkrim - Fix typos
- Rafael Toledo - Fix Gradle plugin badge
- Alberto Ballano - ExceptionRaisedInUnexpectedLocation rule improvements
- Guido Pio Mariotti - Documentation improvement
- Mygod - UnusedImports rule improvement
- Andreas Volkmann - yaml code comments
- glammers - Documentation improvement
- Ahmad El-Melegy - yaml syntax fix
- Arjan Kleene - Add unnecessary apply rule
- Paweł Gajda - Rule improvement: FunctionParameterNaming
- Alistair Sykes - Doc improvement
- Andrew Arnott - UnusedPrivateMember improvement
- Tyler Wong - UnderscoresInNumericLiterals rule
- Daniele Conti - ObjectPropertyNaming improvement
- Nicola Corti - Fixed Suppress of MaxLineLenght
- Michael Lotkowski - Rule improvement: False positive UnusedImport for componentN
- Nuno Caro - Adds TXT report support on Gradle plugin
- Minsuk Eom - Rule fix: PackageNaming
- Jonas Alves - Rule fix: MagicNumber with ignoreNamedArgument and a negative value
- Natig Babayev - Readme improvements
- David Phillips - New rule: MandatoryBracesLoops
- Volkan Şahin - Documentation improvement
- Remco Mokveld - Rename Blacklist/Whitelist to more meaningful names
- Zachary Moore - Rule, cli, gradle plugin, and config improvements
- Veyndan Stuart - New rule: UseEmptyCounterpart; Rule improvement: UselessCallOnNotNull
- Parimatch Tech - New rule: LibraryEntitiesShouldNotBePublic, UnnecessaryFilter
- Chao Zhang - SARIF report format; Rule improvements
- Marcelo Hernandez - New rule: SuspendFunWithFlowReturnType, ObjectExtendsThrowable
- Harold Martin - Rule improvement: ClassOrdering
- Roman Ivanov - Rule improvement: ReturnFromFinally
- Severn Everett - New rule: SleepInsteadOfDelay
- Adam Kobor - New rule: MultilineLambdaItParameter
- Slawomir Czerwinski - Rule improvement: FunctionOnlyReturningConstant
- Ivo Smid - Fix Local development on Windows
Mentions
As mentioned in...
- KotlinConf 2018 - Safe(r) Kotlin Code - Static Analysis Tools for Kotlin by Marvin Ramin
- droidcon NYC 2018 - Static Code Analysis For Kotlin
- Kotlin on Code Quality Tools - by @vanniktech Slides Presentation
- @medium/acerezoluna/static-code-analysis-tools-for-kotlin-in-android
- @medium/annayan/writing-custom-lint-rules-for-your-kotlin-project-with-detekt
- Free Continuous Integration for modern Android apps with CircleCI
- Static code analysis for Kotlin in Android
- The Art of Android DevOps
- Android Basics: Continuous Integration
- Kotlin Static Analysis — why and how?
- Check the quality of Kotlin code
- Kotlin Static Analysis Tools
- Speeding up the detekt task in a multi-project Gradle build (for detekt < 1.7.0)
- SBCARS '18 - Are you still smelling it?: A comparative study between Java and Kotlin language by Flauzino et al.
- Preventing software antipatterns with Detekt
Integrations:
- SonarKotlin
- Codacy
- Gradle plugin that generates ErrorProne, Findbugs, Checkstyle, PMD, CPD, Lint, Detekt & Ktlint Tasks for every subproject
- Java library for parsing report files from static code analysis
- sputnik is a free tool for static code review and provides support for detekt
- Novoda Gradle Static Analysis plugin
- Maven plugin that wraps the Detekt CLI
- Bazel plugin that wraps the Detekt CLI
- Gradle plugin that helps facilitate GitHub PR checking and automatic commenting of violations
- Codefactor
- detekt-hint is a plugin to detekt that provides detection of design principle violations through integration with Danger
- GitHub Action: Detekt All
- IntelliJ Platform Plugin Template
- MuseDev
Custom rules from 3rd parties:
- cph-cachet/detekt-verify-implementation
- detekt-hint is a plugin to detekt that provides detection of design principle violations through integration with Danger
Credits
- JetBrains - Creating IntelliJ + Kotlin
- PMD & Checkstyle & ktlint - Ideas for threshold values and style rules