Skip to content

Commit

Permalink
bugfix/local-variable-loops(#581)
Browse files Browse the repository at this point in the history
### What's done:
 * Fixed bugs
  • Loading branch information
aktsay6 committed Dec 11, 2020
1 parent 65364b9 commit b014c5d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ class ExtensionFunctionsSameNameRule(private val configRules: List<RulesConfig>)
return pairs
}

/**
* FixMe: warning suppressed until https://github.com/cqfn/diKTat/issues/581
*/
@Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION", "TYPE_ALIAS")
@Suppress("UnsafeCallOnNullableType", "TYPE_ALIAS")
private fun collectAllExtensionFunctions(node: ASTNode): SimilarSignatures {
val extensionFunctionList = node.findAllNodesWithSpecificType(FUN).filter { it.hasChildOfType(TYPE_REFERENCE) && it.hasChildOfType(DOT) }
val distinctFunctionSignatures: MutableMap<FunctionSignature, ASTNode> = mutableMapOf() // maps function signatures on node it is used by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class LineLength(private val configRules: List<RulesConfig>) : Rule("line-length
}
}

@Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION")
@Suppress("UnsafeCallOnNullableType")
private fun checkLength(node: ASTNode, configuration: LineLengthConfiguration) {
var offset = 0
node.text.lines().forEach {
Expand Down Expand Up @@ -239,7 +239,7 @@ class LineLength(private val configRules: List<RulesConfig>) : Rule("line-length
}
}

@Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION")
@Suppress("UnsafeCallOnNullableType")
private fun findAllText(astNode: ASTNode): String {
var text = ""
var node = astNode
Expand Down Expand Up @@ -277,7 +277,7 @@ class LineLength(private val configRules: List<RulesConfig>) : Rule("line-length
return text
}

@Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION")
@Suppress("UnsafeCallOnNullableType")
private fun getBraceAndBeforeText(node: ASTNode, prevNode: ASTNode): String {
var text = ""
val par = prevNode.prevSibling { it.elementType == OPERATION_REFERENCE }?.let { LPAR } ?: RPAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SmartCastRule(private val configRules: List<RulesConfig>) : Rule("smart-ca
}

// Divide in is and as expr
@Suppress("TYPE_ALIAS", "LOCAL_VARIABLE_EARLY_DECLARATION")
@Suppress("TYPE_ALIAS")
private fun handleProp(propMap: Map<KtProperty, List<KtNameReferenceExpression>>) {
propMap.forEach { (property, references) ->
val isExpr: MutableList<KtNameReferenceExpression> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SortRule(private val configRules: List<RulesConfig>) : Rule("sort-rule") {
}
}

@Suppress("LOCAL_VARIABLE_EARLY_DECLARATION", "TYPE_ALIAS")
@Suppress("TYPE_ALIAS")
private fun createOrderListOfList(propertyList: List<ASTNode>): List<List<ASTNode>> {
val orderListOfList: MutableList<MutableList<ASTNode>> = mutableListOf()
var oneOrderList = mutableListOf(propertyList.first())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,29 @@ class LocalVariablesRule(private val configRules: List<RulesConfig>) : Rule("loc
}

private fun handleConsecutiveDeclarations(statement: PsiElement, properties: List<KtProperty>) {
val numLinesAfterLastProp = if (properties.last().node.treeNext.elementType == WHITE_SPACE) {
// minus one is needed to except \n after property
properties
val numLinesAfterLastProp =
properties
.last()
.node
.treeNext
.numNewLines() - 1
} else {
0
}
.takeIf { it.elementType == WHITE_SPACE }
?.let {
// minus one is needed to except \n after property
it.numNewLines() - 1
}
?: 0

// need to check that properties are declared consecutively with only maybe empty lines
properties
.sortedBy { it.node.getLineNumber() }
.let { it as MutableList }
.zip(((properties.size - 1) downTo 0).map { it + numLinesAfterLastProp })
.zip(
(properties.size - 1 downTo 0).map { it + numLinesAfterLastProp }
)
.forEachIndexed { index, (property, offset) ->
if (index != properties.lastIndex) {
checkLineNumbers(property, statement.node.getLineNumber(), offset)
} else {
// since offset after last property is calculated in this method, we pass offset = 0
checkLineNumbers(property, statement.node.getLineNumber(), 0)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ fun List<ASTNode>.handleIncorrectOrder(
* This method returns text of this [ASTNode] plus text from it's siblings after last and until next newline, if present in siblings.
* I.e., if this node occupies no more than a single line, this whole line or it's part will be returned.
*/
@Suppress("LOCAL_VARIABLE_EARLY_DECLARATION", "WRONG_NEWLINES")
@Suppress("WRONG_NEWLINES")
fun ASTNode.extractLineOfText(): String {
var text: MutableList<String> = mutableListOf()
siblings(false)
Expand Down Expand Up @@ -743,7 +743,6 @@ fun ASTNode.getLineNumber(): Int =
* This function calculates line number instead of using cached values.
* It should be used when AST could be previously mutated by auto fixers.
*/
@Suppress("LOCAL_VARIABLE_EARLY_DECLARATION")
private fun ASTNode.calculateLineNumber(): Int {
var count = 0
// todo use runningFold or something similar when we migrate to apiVersion 1.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class LocalVariablesWarnTest : LintTestBase(::LocalVariablesRule) {

@Test
@Tag(WarningNames.LOCAL_VARIABLE_EARLY_DECLARATION)
fun `should emit only one warning when same variables are used more than once`() {
fun `should not trigger on properties`() {
lintMethod(
"""
|private fun checkDoc(node: ASTNode, warning: Warnings) {
Expand Down Expand Up @@ -583,12 +583,6 @@ class LocalVariablesWarnTest : LintTestBase(::LocalVariablesRule) {
| val extensionFunctionsPairs = mutableListOf<Pair<ExtensionFunction, ExtensionFunction>>() // pairs extension functions with same signature
|
| extensionFunctionList.forEach { func ->
| val functionName = (func.psi as KtNamedFunction).name!!
| // List<String> is used to show param names in warning
| val params = (func.getFirstChildWithType(VALUE_PARAMETER_LIST)!!.psi as KtParameterList).parameters.map { it.name!! }
| val returnType = func.findChildAfter(COLON, TYPE_REFERENCE)?.text
| val className = func.findChildBefore(DOT, TYPE_REFERENCE)!!.text
| val signature = FunctionSignature(functionName, params, returnType)
| if (distinctFunctionSignatures.contains(signature)) {
| val secondFuncClassName = distinctFunctionSignatures[signature]!!.findChildBefore(DOT, TYPE_REFERENCE)!!.text
| extensionFunctionsPairs.add(Pair(
Expand Down

0 comments on commit b014c5d

Please sign in to comment.