Diktat
Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
Install / Use
/learn @saveourtool/DiktatREADME
DiKTat is a strict coding standard for Kotlin, consisting of a collection of Kotlin code style rules implemented as Abstract Syntax Tree (AST) visitors built on top of KTlint. It serves the purpose of detecting and automatically fixing code smells in the Continuous Integration/Continuous Deployment (CI/CD) process. You can find the comprehensive list of supported rules and inspections here.
DiKTat has gained recognition and has been added to the lists of static analysis tools, kotlin-awesome, and kompar. We extend our gratitude to the community for this support!
See first
| | | | | | | | --- | --- | --- | --- | --- | --- | |Codestyle|Inspections | Examples | Demo | White Paper | Groups of Inspections |
Why Choose DiKTat for CI/CD?
While there are other tools like detekt and ktlint performing static analysis, you might wonder why DiKTat is necessary. Here are the key reasons:
-
More Inspections: DiKTat boasts over 100 inspections tightly coupled with its Codestyle.
-
Unique Inspections: DiKTat introduces unique inspections not found in other linters.
-
Highly Configurable: Every inspection is highly configurable, allowing customization and suppression. Check configuration options and suppression.
-
Strict Codestyle: DiKTat enforces a detailed Codestyle that can be adopted and applied in your project.
Run as CLI-application
Download binary
-
Download diKTat manually: here
OR use
curl:curl -sSLO https://github.com/saveourtool/diktat/releases/download/v2.0.0/diktat && chmod a+x diktat
For Windows only. Download diKTat.cmd manually: here
Run diKTat
Finally, run KTlint (with diKTat injected) to check your '*.kt' files in 'dir/your/dir':
$ ./diktat "dir/your/dir/**/*.kt"
On Windows
diktat.bat "dir/your/dir/**/*.kt"
To autofix all code style violations, use --mode fix option.
Run with Maven using diktat-maven-plugin
You can see how it is configured in our examples:
<details> <summary>Add this plugin to your pom.xml:</summary> <plugin>
<groupId>com.saveourtool.diktat</groupId>
<artifactId>diktat-maven-plugin</artifactId>
<version>${diktat.version}</version>
<executions>
<execution>
<id>diktat</id>
<phase>none</phase>
<goals>
<goal>check</goal>
<goal>fix</goal>
</goals>
<configuration>
<inputs>
<input>${project.basedir}/src/main/kotlin</input>
<input>${project.basedir}/src/test/kotlin</input>
</inputs>
<diktatConfigFile>diktat-analysis.yml</diktatConfigFile>
<excludes>
<exclude>${project.basedir}/src/test/kotlin/excluded</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</details>
To run diktat in only-check mode use command $ mvn diktat:check@diktat.
To run diktat in autocorrect mode use command $ mvn diktat:fix@diktat.
Requesting a specific Maven executionId on the command line (the trailing
diktat in the above example) may be essential in these cases:
-
In your
<details>pom.xml, you have multiple executions with different configurations (e. g.: multiple rule sets):
</details><executions> <execution> <id>diktat-basic</id> <configuration> <diktatConfigFile>diktat-analysis.yml</diktatConfigFile> </configuration> </execution> <execution> <id>diktat-advanced</id> <configuration> <diktatConfigFile>diktat-analysis-advanced.yml</diktatConfigFile> </configuration> </execution> </executions> -
Your YAML file with DiKTat rules has a non-default name and/or resides in a non-default location:
<details>
</details><executions> <execution> <id>diktat</id> <configuration> <diktatConfigFile>/non/default/rule-set-file.yml</diktatConfigFile> </configuration> </execution> </executions>- You can omit the
diktatConfigFileor if it points to non-existed file then DiKTat runs with default configuration.
- You can omit the
If you omit the executionId:
$ mvn diktat:check
— the plug-in will use the default configuration and search for
diktat-analysis.yml file in the project directory (you can still customize the
rule sets by editing the YAML file).
Run with Gradle using diktat-gradle-plugin
Requires a gradle version no lower than 7.0
You can see how the plugin is configured in our examples:
<details> <summary>Add this plugin to your `build.gradle.kts`:</summary>plugins {
id("com.saveourtool.diktat") version "2.0.0"
}
Note If you want to apply the plugin to multi-module projects"
import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin plugins { id("com.saveourtool.diktat") version "2.0.0" apply false } allprojects { apply<DiktatGradlePlugin>() }
You can then configure diktat using diktat extension:
diktat {
inputs {
include("src/**/*.kt") // path matching this pattern (per PatternFilterable) that will be checked by diktat
exclude("src/test/kotlin/excluded/**") // path matching this pattern will not be checked by diktat
}
debug = true // turn on debug logging
}
Also in diktat extension you can configure different reporters and their output. You can specify json, html, sarif, plain (default).
If output is set, it should be a file path. If not set, results will be printed to stdout.
You can specify multiple reporters.
If no reporter is specified, plain will be used with stdout as output.
diktat {
reporters {
plain()
json()
html {
output = file("someFile.html")
}
// checkstyle()
// sarif()
// gitHubActions()
}
}
</details>
You can run diktat checks using task ./gradlew diktatCheck and automatically fix errors with task ./gradlew diktatFix.
Run with Spotless
Spotless is a linter aggregator.
Gradle
Diktat can be run via spotless-gradle-plugin since version 5.10.0
<details> <summary>Add this plugin to your build.gradle.kts</summary>plugins {
id("com.diffplug.spotless") version "5.10.0"
}
spotless {
kotlin {
diktat()
}
kotlinGradle {
diktat()
}
}
</details>
<details>
<summary>You can provide a version and configuration path manually as configFile.</summary>
spotless {
kotlin {
diktat("2.0.0").configFile("full/path/to/diktat-analysis.yml")
}
}
</details>
Maven
Diktat can be run via spotless-maven-plugin since version 2.8.0
<details> <summary>Add this plugin to your pom.xml</summary><plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotl
