saveourtool/diktat

`WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR` false positive when using property references inside an accessor

Open

#1,464 创建于 2022年7月20日

在 GitHub 查看
 (0 评论) (0 反应) (1 负责人)Kotlin (40 fork)github user discovery
buggood first issue

仓库指标

Star
 (571 star)
PR 合并指标
 (PR 指标待抓取)

描述

Minimal repro

package com.example

interface Interface {
    val state: Int
}

abstract class Primitive : Interface

class Composite : Interface {
    private val primitives = emptyList<Primitive>()

    override val state: Int
        get() =
            42 + primitives.sumOf(Interface::state)
}

Even a smaller repro

package com.example

class Klazz {
    private val children = emptyList<Klazz>()

    val state: Int
        get() =
            42 + children.sumOf(Klazz::state)
}

Observed behavior

file.kt:13:9: [WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR] use `field` keyword instead of property name inside property accessors: get() =... (diktat-ruleset:getter-setter-fields)

An additional problem with the above error is that the special field identifier is only allowed when there's a backing field (i. e. the property has been initialized), e. g.:

    val state: Int = 42
        get() {
            println(field)
            return 44
        }

Environment information

  • diktat version: 1.2.2-SNAPSHOT at a17825ae283f3f01eef2c664afea7fdd54eea185

贡献者指南