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

Bugfix. Unhandled ArrayStoreException when string template has [] operator inside #513

Merged
merged 4 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.rules

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.ARRAY_ACCESS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.BINARY_WITH_TYPE
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
Expand Down Expand Up @@ -93,12 +94,21 @@ class StringTemplateFormatRule(private val configRules: List<RulesConfig>) : Rul
.singleOrNull()
?.treeParent
?.elementType == LONG_STRING_TEMPLATE_ENTRY
return if (onlyOneRefExpr) {
!(node.treeNext.text.first().isLetterOrDigit() // checking if first letter is valid

val isArrayAccessExpression = node // this should be omitted in previous expression, used for safe warranties
.findAllNodesWithSpecificType(REFERENCE_EXPRESSION)
.singleOrNull()
?.treeParent
?.elementType == ARRAY_ACCESS_EXPRESSION

return if (onlyOneRefExpr && !isArrayAccessExpression) {
(!(node.treeNext.text.first().isLetterOrDigit() // checking if first letter is valid
|| node.treeNext.text.startsWith("_"))
|| node.treeNext.elementType == CLOSING_QUOTE
} else {
|| node.treeNext.elementType == CLOSING_QUOTE)
} else if(!isArrayAccessExpression) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you will need to check for more corner cases - this can be not the only problem here

node.hasAnyChildOfTypes(FLOAT_CONSTANT, INTEGER_CONSTANT) // it also fixes "${1.0}asd" cases
} else {
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,18 @@ class StringTemplateRuleWarnTest : LintTestBase(::StringTemplateFormatRule) {
""".trimMargin()
)
}

@Test
@Tag(STRING_TEMPLATE_CURLY_BRACES)
fun `should not trigger on array access`() {
lintMethod(
"""
|class Some {
| fun some() {
| val copyTestFile = "${'$'}{arr[0]}"
| }
|}
""".trimMargin()
)
}
}