package com.example
class MutableTableContainer {
private var _table: Map<String, Int>? = null
val table: Map<String, Int>
get() {
if (_table == null) {
_table = hashMapOf()
}
return _table ?: throw AssertionError("Set to null by another thread")
}
}
Diktat 1.2.5 warns:
MutableTableContainer.kt:4:17: [VARIABLE_NAME_INCORRECT_FORMAT] variable name should be in lowerCamelCase and should contain only latin (ASCII) letters or numbers and should start from lower letter: _table (diktat-ruleset:identifier-naming)
Probably we should make an exception for backing properties whenever there's an explicit backing field with the corresponding name (xyz → _xyz) and of the same type.
Requirements:
the backing field should be private (or have a lowervisibility than that of the property);
the backing field should have a matching name (xyz → _xyz);
the backing field should have the same type as the property (either convert both types to a simple name, or resolve to a FQN before you compare; nullable subtypes should be permitted: Map<String, Int> → Map<String, Int>?);
the backing field should have no accessors/modifiers of its own;
the property should have at least an accessor, or a modifier, or both.
Related:
[x] #1709
Expected behavior
No warning should have been reported.
Observed behavior
A warning was reported.
Steps to Reproduce
Check the above code example with Diktat.
Environment information
diktat version: 1.2.5
build tool (maven/gradle): any
how is diktat run (CLI, plugin, etc.): doesn't matter
Describe the bug
Consider this idiomatic example taken directly from https://kotlinlang.org:
Diktat 1.2.5 warns:
Probably we should make an exception for backing properties whenever there's an explicit backing field with the corresponding name (
xyz
→_xyz
) and of the same type.Requirements:
private
(or have a lower visibility than that of the property);xyz
→_xyz
);Map<String, Int>
→Map<String, Int>?
);Related:
Expected behavior
No warning should have been reported.
Observed behavior
A warning was reported.
Steps to Reproduce
Check the above code example with Diktat.
Environment information