Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialVersionUID should be ignored in CONSTANT_UPPERCASE #1849

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
)

private fun checkVariableName(node: ASTNode): List<ASTNode> {
val exceptionConstNames = setOf("serialVersionUID")
// special case for Destructuring declarations that can be treated as parameters in lambda:
var namesOfVariables = extractVariableIdentifiers(node)

Expand All @@ -178,7 +179,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
// check for constant variables - check for val from companion object or on global file level
// it should be in UPPER_CASE, no need to raise this warning if it is one-letter variable
if (node.isConstant()) {
if (!variableName.text.isUpperSnakeCase() && variableName.text.length > 1) {
if (!variableName.text.isUpperSnakeCase() && variableName.text.length > 1 && !exceptionConstNames.contains(variableName.text)) {
CONSTANT_UPPERCASE.warnOnlyOrWarnAndFix(
configRules = configRules,
emit = emitWarn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,36 @@ class IdentifierNamingWarnTest : LintTestBase(::IdentifierNaming) {
)
}

@Test
@Tag(WarningNames.CONSTANT_UPPERCASE)
fun `serialVersionUID should be ignored`() {
lintMethod(
"""
class TestSerializableClass() : Serializable {
companion object {
private const val serialVersionUID: Long = -1
}
}
""".trimIndent()
)
}

@Test
@Tag(WarningNames.CONSTANT_UPPERCASE)
fun `should trigger when the name is not exception`() {
val code =
"""
class TestSerializableClass() : Serializable {
companion object {
private const val serialVersion: Long = -1
}
}
""".trimIndent()
lintMethod(code,
DiktatError(3, 27, ruleId, "${CONSTANT_UPPERCASE.warnText()} serialVersion", true)
)
}

@Test
@Tags(Tag(WarningNames.IDENTIFIER_LENGTH), Tag(WarningNames.VARIABLE_NAME_INCORRECT))
fun `check variable length (check - negative)`() {
Expand Down