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
544 stars 39 forks source link

serialVersionUID should be ignored in CONSTANT_UPPERCASE #1847

Closed illarionov closed 11 months ago

illarionov commented 11 months ago

Describe the bug

This code:

class TestSerializableClass() : Serializable
    companion object {
        private const val serialVersionUID: Long = -1
    }
}

triggers the error:

[CONSTANT_UPPERCASE] <val> properties from companion object or on file level mostly in all cases are constants - please use upper snake case for them: serialVersionUID (diktat-ruleset:identifier-naming)

Expected behavior

serialVersionUID should be ignored

Environment information

kgevorkyan commented 11 months ago

Hello @illarionov!

Thanks for reporting, but I'm not sure. whether there is a error

However, you can use @Suppress annotation, to ignore this rule, if you want to use this variable directly with this name

    companion object {
        @Suppress("CONSTANT_UPPERCASE")
        private const val serialVersionUID: Long = -1
    }
illarionov commented 11 months ago

This is a feature request, actually.
`serialVersionUID" this is the field name that cannot be changed when implementing the Serializable interface. Adding Suppress annotation to every field impair readability.

orchestr7 commented 11 months ago

Good point actually. This can be harcoded easily in naming rule. This is a good corner case, may be can be reflected in our guide https://github.com/saveourtool/diktat/blob/master/info/guide/diktat-coding-convention.md

orchestr7 commented 11 months ago

Hello @illarionov!

Thanks for reporting, but I'm not sure. whether there is a error

However, you can use @Suppress annotation, to ignore this rule, if you want to use this variable directly with this name

    companion object {
        @Suppress("CONSTANT_UPPERCASE")
        private const val serialVersionUID: Long = -1
    }

the same can be done by @get:JvmName("serialVersionUID"), but yes, it looks ugly and not user friendly