diktat
diktat copied to clipboard
`ANNOTATION_NEW_LINE` not applied to annotations on primary constructors
Describe the bug
ANNOTATION_NEW_LINE
doesn't apply to primary constructor annotations and/or WRONG_INDENTATION
is incorrectly applied when a class only consists of a primary constructor.
Given the following class
open class ExampleExtension
@Inject
constructor(objects: ObjectFactory) {
val name: Property<String> = objects.property()
val mainClass: Property<String> = objects.property()
val defaultJvmArgs: ListProperty<String> = objects.listProperty()
val localEnvVars: MapProperty<String, String> = objects.mapProperty()
}
and the configuration
- name: WRONG_INDENTATION
enabled: true
configuration:
indentationSize: 2
Expected behavior
I would expect the above code to pass analysis as is.
Observed behavior
Currently fails with the WRONG_INDENTATION
due to the @Inject
and following lines being (correctly) indented
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 2 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 2 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 2 (diktat-ruleset:indentation)
This can be bypassed by moving the annotated constructor to the same line as the class declaration
open class ExampleExtension @Inject constructor(objects: ObjectFactory) {
val name: Property<String> = objects.property()
val mainClass: Property<String> = objects.property()
val defaultJvmArgs: ListProperty<String> = objects.listProperty()
val localEnvVars: MapProperty<String, String> = objects.mapProperty()
}
However I would expect this to now fail the ANNOTATION_NEW_LINE
rules as the @Inject
annotation isn't on a new line, but it doesn't.
Steps to Reproduce
See above.
Environment information
- diktat version: 2.0.0
- build tool (maven/gradle): gradle
- how is diktat run (CLI, plugin, etc.): ./gradlew diktatCheck
- kotlin version: 1.9.21
Hi @colesnodgrass, thanks for raising the issue.
According to Diktat's code style, this behavior is expected and ANNOTATION_NEW_LINE
should not be applied to a primary constructor.
We will investigate it to try to make it configurable to support your format