Multiple annotations should be on a separate line than the annotated declaration; annotations with parameters should each be on separate lines; annotations should be followed by a space
Rule-id: annotation
Rule-id: argument-list-wrapping
When wrapping chained calls .
, ?.
and ?:
should be placed on the next line
Rule id: chain-wrapping
Enum entry names should be uppercase underscore-separated names.
Rule id: enum-entry-name-case
Files containing only one toplevel domain should be named according to that element.
Rule id: filename
Ensures consistent usage of a newline at the end of each file.
This rule can be configured with .editorconfig
property insert_final_newline
.
Rule id: final-newline
Imports ordered consistently (see Custom ktlint EditorConfig properties for more)
Rule id: import-ordering
Indentation formatting - respects .editorconfig
indent_size
with no continuation indent (see EditorConfig section for more).
Rule id: indent
Ensures that lines do not exceed the given length of .editorconfig
property max_line_length
(see EditorConfig section for more). This rule does not apply in a number of situations. For example, in the case a line exceeds the maximum line length due to and comment that disables ktlint rules than that comment is being ignored when validating the length of the line. The .editorconfig
property ktlint_ignore_back_ticked_identifier
can be set to ignore identifiers which are enclosed in backticks, which for example is very useful when you want to allow longer names for unit tests.
Rule id: max-line-length
Consistent order of modifiers
Rule id: modifier-order
Braces required for multiline if/else statements.
Rule id: multiline-if-else
No blank lines before }
.
Rule id: no-blank-line-before-rbrace
Rule id: no-blank-lines-in-chained-method-calls
Rule id: no-consecutive-blank-lines
Rule id: no-empty-class-body
Rule id: no-empty-first-line-in-method-block
Disallows line breaks after the else keyword if that could lead to confusion, for example:
if (conditionA()) {
doSomething()
} else
if (conditionB()) {
doAnotherThing()
}
Rule id: no-line-break-after-else
When a line is broken at an assignment (=
) operator the break comes after the symbol.
Rule id: no-line-break-before-assignment
Except in indentation and in KDoc's it is not allowed to have multiple consecutive spaces.
Rule id: no-multi-spaces
No semicolons (unless used to separate multiple statements on the same line).
Rule id: no-semi
Rule id: no-trailing-spaces
The Unit
type is not allowed as return type of a function.
returns (fun fn {}
instead of fun fn: Unit {}
)
=== ":material-heart: Ktlint"
```kotlin
fun fn() {}
```
=== ":material-heart-off-outline: Disallowed"
```kotlin
fun fn(): Unit {}
```
Rule id: no-unit-return
Rule id: no-unused-imports
No wildcard imports except imports listed in .editorconfig
property ij_kotlin_packages_to_use_import_on_demand
.
Rule id: no-wildcard-imports
Rule id: package-name
When class/function signature doesn't fit on a single line, each parameter must be on a separate line
Rule id: parameter-list-wrapping
Consistent string templates ($v
instead of ${v}
, ${p.v}
instead of ${p.v.toString()}
)
Rule id: string-template
Consistent removal (default) or adding of trailing comma's on call site.
!!! important
KtLint uses the IntelliJ IDEA .editorconfig
property ij_kotlin_allow_trailing_comma_on_call_site
to configure the rule. When this property is enabled, KtLint enforces the usage of the trailing comma at call site while IntelliJ IDEA default formatter only allows to use the trailing comma but leaves it to the developer's discretion to actually use it (or not). KtLint values consistent formatting more than a per-situation decision.
!!! note
In KtLint 0.48.x the default value for using the trailing comma on call site has been changed to true
except when codestyle android
is used.
Although the Kotlin coding conventions](https://kotlinlang.org/docs/reference/coding-conventions.html#names-for-test-methods) leaves it to the developer's discretion to use trailing comma's on the call site, it also states that usage of trailing commas has several benefits:
* It makes version-control diffs cleaner – as all the focus is on the changed value.
* It makes it easy to add and reorder elements – there is no need to add or delete the comma if you manipulate elements.
* It simplifies code generation, for example, for object initializers. The last element can also have a comma.
Rule id: trailing-comma-on-call-site
Consistent removal (default) or adding of trailing comma's on declaration site.
!!! important
KtLint uses the IntelliJ IDEA .editorconfig
property ij_kotlin_allow_trailing_comma
to configure the rule. When this property is enabled, KtLint enforces the usage of the trailing comma at declaration site while IntelliJ IDEA default formatter only allows to use the trailing comma but leaves it to the developer's discretion to actually use it (or not). KtLint values consistent formatting more than a per-situation decision.
!!! note
In KtLint 0.48.x the default value for using the trailing comma on declaration site has been changed to true
except when codestyle android
is used.
The Kotlin coding conventions](https://kotlinlang.org/docs/reference/coding-conventions.html#names-for-test-methods) encourages the usage of trailing comma's on the declaration site, but leaves it to the developer's discretion to use trailing comma's on the call site. But next to this, it also states that usage of trailing commas has several benefits:
* It makes version-control diffs cleaner – as all the focus is on the changed value.
* It makes it easy to add and reorder elements – there is no need to add or delete the comma if you manipulate elements.
* It simplifies code generation, for example, for object initializers. The last element can also have a comma.
Rule id: trailing-comma-on-declaration-site
Inserts missing newlines (for example between parentheses of a multi-line function call).
Rule id: wrapping
Annotations should be separated by a single line break.
Rule id: annotation-spacing
Consistent spacing around colon.
Rule id: colon-spacing
Consistent spacing around comma.
Rule id: comma-spacing
The end of line comment sign //
should be preceded and followed by exactly a space.
Rule id: comment-spacing
Consistent spacing around curly braces.
Rule id: curly-spacing
Consistent spacing around dots.
Rule id: dot-spacing
No spaces around ::
.
Rule id: double-colon-spacing
Consistent spacing around keywords.
Rule id: keyword-spacing
Consistent spacing around operators.
Rule id: op-spacing
Consistent spacing around parenthesis.
Rule id: paren-spacing
Consistent spacing around range operators.
Rule id: range-spacing
No spaces around angle brackets when used for typing.
Rule id: spacing-around-angle-brackets
Declarations with annotations should be separated by a blank line.
Rule id: spacing-between-declarations-with-annotations
Declarations with comments should be separated by a blank line.
Rule id: spacing-between-declarations-with-comments
No spaces around unary operators.
Rule id: unary-op-spacing