saveourtool / diktat

Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
https://diktat.saveourtool.com
MIT License
524 stars 39 forks source link

`ANNOTATION_NEW_LINE` not applied to annotations on primary constructors #1887

Open colesnodgrass opened 9 months ago

colesnodgrass commented 9 months ago

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

nulls commented 9 months ago

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