-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/kotlin-1.4
- Loading branch information
Showing
19 changed files
with
207 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/OverloadingArgumentsFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.FUN | ||
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER | ||
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_OVERLOADING_FUNCTION_ARGUMENTS | ||
import org.cqfn.diktat.ruleset.utils.allSiblings | ||
import org.cqfn.diktat.ruleset.utils.findChildAfter | ||
import org.cqfn.diktat.ruleset.utils.findChildBefore | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtFunction | ||
import org.jetbrains.kotlin.psi.psiUtil.startOffset | ||
|
||
class OverloadingArgumentsFunction(private val configRules: List<RulesConfig>) : Rule("overloading-default-values") { | ||
|
||
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) | ||
private var isFixMode: Boolean = false | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
isFixMode = autoCorrect | ||
emitWarn = emit | ||
|
||
if (node.elementType == FUN) { | ||
checkFun(node.psi as KtFunction) | ||
} | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun checkFun(funPsi: KtFunction) { | ||
val allOverloadFunction = funPsi.node.allSiblings(withSelf = false) | ||
.asSequence() | ||
.filter { it.elementType == FUN } | ||
.map { it.psi as KtFunction } | ||
.filter { it.nameIdentifier!!.text == funPsi.nameIdentifier!!.text && it.valueParameters.containsAll(funPsi.valueParameters) } | ||
.filter { funPsi.node.findChildBefore(IDENTIFIER, TYPE_REFERENCE)?.text == it.node.findChildBefore(IDENTIFIER, TYPE_REFERENCE)?.text } | ||
.filter {funPsi.node.findChildAfter(IDENTIFIER, TYPE_REFERENCE)?.text == it.node.findChildAfter(IDENTIFIER, TYPE_REFERENCE)?.text } | ||
.toList() | ||
if (allOverloadFunction.isNotEmpty()) { | ||
WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warn(configRules, emitWarn, isFixMode, funPsi.node.findChildByType(IDENTIFIER)!!.text, funPsi.startOffset, funPsi.node) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.../src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/OverloadingArgumentsFunctionWarnTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package org.cqfn.diktat.ruleset.chapter5 | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_OVERLOADING_FUNCTION_ARGUMENTS | ||
import org.cqfn.diktat.ruleset.rules.OverloadingArgumentsFunction | ||
import org.cqfn.diktat.util.LintTestBase | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class OverloadingArgumentsFunctionWarnTest : LintTestBase(::OverloadingArgumentsFunction) { | ||
|
||
private val ruleId = "$DIKTAT_RULE_SET_ID:overloading-default-values" | ||
|
||
@Test | ||
@Tag(WarningNames.WRONG_OVERLOADING_FUNCTION_ARGUMENTS) | ||
fun `check simple example`() { | ||
lintMethod( | ||
""" | ||
|fun foo() {} | ||
| | ||
|fun foo(a: Int) {} | ||
| | ||
|fun goo(a: Double) {} | ||
| | ||
|fun goo(a: Float, b: Double) {} | ||
| | ||
|fun goo(b: Float, a: Double, c: Int) {} | ||
| | ||
|@Suppress("WRONG_OVERLOADING_FUNCTION_ARGUMENTS") | ||
|fun goo(a: Float) | ||
| | ||
|fun goo(a: Double? = 0.0) {} | ||
| | ||
|override fun goo() {} | ||
| | ||
|class A { | ||
| fun foo() {} | ||
|} | ||
| | ||
|abstract class B { | ||
| abstract fun foo(a: Int) | ||
| | ||
| fun foo(){} | ||
|} | ||
""".trimMargin(), | ||
LintError(1,1, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} foo", false), | ||
LintError(16,1, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} goo", false), | ||
LintError(25,4, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} foo", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.WRONG_OVERLOADING_FUNCTION_ARGUMENTS) | ||
fun `check simple`() { | ||
lintMethod( | ||
""" | ||
private fun isComparisonWithAbs(psiElement: PsiElement) = | ||
when (psiElement) { | ||
is KtBinaryExpression -> psiElement.isComparisonWithAbs() | ||
is KtDotQualifiedExpression -> psiElement.isComparisonWithAbs() | ||
else -> false | ||
} | ||
private fun KtBinaryExpression.isComparisonWithAbs() = | ||
takeIf { it.operationToken in comparisonOperators } | ||
?.run { left as? KtCallExpression ?: right as? KtCallExpression } | ||
?.run { calleeExpression as? KtNameReferenceExpression } | ||
?.getReferencedName() | ||
?.equals("abs") | ||
?: false | ||
private fun KtBinaryExpression.isComparisonWithAbs(a: Int) = | ||
takeIf { it.operationToken in comparisonOperators } | ||
?.run { left as? KtCallExpression ?: right as? KtCallExpression } | ||
?.run { calleeExpression as? KtNameReferenceExpression } | ||
?.getReferencedName() | ||
?.equals("abs") | ||
?: false | ||
private fun KtBinaryExpression.isComparisonWithAbs(a: Int): Boolean { | ||
return takeIf { it.operationToken in comparisonOperators } | ||
?.run { left as? KtCallExpression ?: right as? KtCallExpression } | ||
?.run { calleeExpression as? KtNameReferenceExpression } | ||
?.getReferencedName() | ||
?.equals("abs") | ||
?: false | ||
} | ||
""".trimMargin(), | ||
LintError(8,21, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} isComparisonWithAbs", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.WRONG_OVERLOADING_FUNCTION_ARGUMENTS) | ||
fun `check methods with different return types`() { | ||
lintMethod( | ||
""" | ||
private fun KtBinaryExpression.isComparisonWithAbs(a: Int): Boolean { | ||
return takeIf { it.operationToken in comparisonOperators } | ||
?.run { left as? KtCallExpression ?: right as? KtCallExpression } | ||
?.run { calleeExpression as? KtNameReferenceExpression } | ||
?.getReferencedName() | ||
?.equals("abs") | ||
?: false | ||
} | ||
private fun KtBinaryExpression.isComparisonWithAbs(a: Int): Int { | ||
val q = takeIf { it.operationToken in comparisonOperators } | ||
?.run { left as? KtCallExpression ?: right as? KtCallExpression } | ||
?.run { calleeExpression as? KtNameReferenceExpression } | ||
?.getReferencedName() | ||
?.equals("abs") | ||
?: false | ||
if (q) return 10 | ||
return 11 | ||
} | ||
""".trimMargin() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.