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. NPE in TrivialPropertyAccessors rule #512

Merged
merged 6 commits into from
Nov 11, 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 @@ -32,6 +32,7 @@ class TrivialPropertyAccessors(private val configRules: List<RulesConfig>) : Rul

companion object {
private val EXCESS_CHILDREN_TYPES = listOf(LBRACE, RBRACE, WHITE_SPACE, EOL_COMMENT, BLOCK_COMMENT)
private const val ONE_CHILD_IN_ARRAY = 1
}

override fun visit(node: ASTNode,
Expand All @@ -56,12 +57,12 @@ class TrivialPropertyAccessors(private val configRules: List<RulesConfig>) : Rul
@Suppress("UnsafeCallOnNullableType")
private fun handleSetAccessor(node: ASTNode) {
val valueParamName = node
.getFirstChildWithType(VALUE_PARAMETER_LIST)!!
.firstChildNode
.getIdentifierName()!!
.text
.getFirstChildWithType(VALUE_PARAMETER_LIST)
?.firstChildNode
?.getIdentifierName()
?.text

if (node.hasChildOfType(BLOCK)) {
if (node.hasChildOfType(BLOCK) && !valueParamName.isNullOrEmpty()) {
val block = node.getFirstChildWithType(BLOCK)!!

val blockChildren = block.getChildren(null).filter { it.elementType !in EXCESS_CHILDREN_TYPES }
Expand All @@ -82,6 +83,8 @@ class TrivialPropertyAccessors(private val configRules: List<RulesConfig>) : Rul
val references = node.findAllNodesWithSpecificType(REFERENCE_EXPRESSION)
if (references.singleOrNull()?.text == "field") {
raiseWarning(node)
} else if (node.getChildren(null).size == ONE_CHILD_IN_ARRAY) {
raiseWarning(node)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cqfn.diktat.ruleset.chapter6

import generated.WarningNames
import generated.WarningNames.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED
import org.cqfn.diktat.util.FixTestBase
import org.cqfn.diktat.ruleset.rules.TrivialPropertyAccessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,31 @@ class TrivialPropertyAccessorsWarnTest : LintTestBase(::TrivialPropertyAccessors
""".trimMargin()
)
}

@Test
@Tag(TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED)
fun `should not trigger on private setter`() {
lintMethod(
"""
|class Test {
| var testName: String? = null
| private set
|}
""".trimMargin()
)
}

@Test
@Tag(TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED)
fun `should trigger on getter without braces`() {
lintMethod(
"""
|class Test {
| val testName = 0
| get
|}
""".trimMargin(),
LintError(3, 8, ruleId, "${Warnings.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED.warnText()} get", true)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ class Some {
var propNotChange: Int = 7
get() { return someCoolLogic(field) }
set(value) { anotherCoolLogic(value) }

var testName: String? = null
private set

val x = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ class Some {
var propNotChange: Int = 7
get() { return someCoolLogic(field) }
set(value) { anotherCoolLogic(value) }

var testName: String? = null
private set

val x = 0
get
}