Skip to content

Commit

Permalink
Merge branch 'master' into feature/kotlin-1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
petertrr authored Oct 13, 2020
2 parents 91d7958 + 65d36d3 commit f24fea1
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 85 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/diktat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ jobs:
key: ${{ runner.os }}-maven-diktat-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-diktat-
- name: Run diktat itself via ktlint
# fixme: change to diktat:check when diktat plugin is released
run: mvn -B antrun:run@diktat
- name: Run diktat via maven plugin
run: mvn -B diktat:check@diktat
5 changes: 2 additions & 3 deletions .github/workflows/functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ master ]

env:
DIKTAT_VERSION: 0.1.1
DIKTAT_VERSION: 0.1.2
KTLINT_VERSION: 0.39.0

jobs:
Expand Down Expand Up @@ -43,8 +43,7 @@ jobs:
- name: Run diKTat from maven
continue-on-error: true
# fixme: change to diktat:check when diktat plugin is released
run: mvn -B antrun:run@functional-test &> out
run: mvn -B diktat:check@functional-test &> out

- name: Check maven
run: |
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ The full list of available supported rules and inspections is [here](info/availa
# another option is "brew install ktlint"
```

2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.1.1/diktat.jar)
2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.1.2/diktat.jar)

**OR** use curl:
```bash
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.1/diktat-0.1.1.jar
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.2/diktat-0.1.2.jar
```

3. Finally, run KTlint (with diKTat injected) to check your `*.kt` files in `dir/your/dir`:
Expand Down Expand Up @@ -100,7 +100,7 @@ Add this plugin to your pom.xml:
<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-rules</artifactId>
<version>0.1.1</version> <!-- replace it with diktat latest version -->
<version>0.1.2</version> <!-- replace it with diktat latest version -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
Expand All @@ -125,8 +125,7 @@ To run diktat to check/fix code style - run `$ mvn antrun:run@diktat`.
### Use the new diktat-maven-plugin

You can see how it is configured in our project for self-checks: [pom.xml](pom.xml).
This plugin should be available since version 0.1.2. It requires less configuration but may contain bugs.
If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues).
This plugin is available since version 0.1.2. If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues).

Add this plugin to your pom.xml:
<details>
Expand Down Expand Up @@ -179,7 +178,7 @@ dependencies {
}
// diktat ruleset
ktlint("org.cqfn.diktat:diktat-rules:0.1.1")
ktlint("org.cqfn.diktat:diktat-rules:0.1.2")
}
val outputDir = "${project.buildDir}/reports/diktat/"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
}

// diktat ruleset
ktlint("org.cqfn.diktat:diktat-rules:0.1.1")
ktlint("org.cqfn.diktat:diktat-rules:0.1.2")
}

val outputDir = "${project.buildDir}/reports/diktat/"
Expand Down
6 changes: 5 additions & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,8 @@
- name: NESTED_BLOCK
enabled: true
configuration:
maxNestedBlockQuantity: '4'
maxNestedBlockQuantity: '4'
# Checks that function use default values, instead overloading
- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS
enabled: true
configuration: {}
2 changes: 1 addition & 1 deletion diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion diktat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>diktat-parent</artifactId>
<groupId>org.cqfn.diktat</groupId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>

</parent>
<properties>
Expand Down
2 changes: 2 additions & 0 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ object WarningNames {
const val TOO_MANY_PARAMETERS: String = "TOO_MANY_PARAMETERS"

const val NESTED_BLOCK: String = "NESTED_BLOCK"

const val WRONG_OVERLOADING_FUNCTION_ARGUMENTS: String = "WRONG_OVERLOADING_FUNCTION_ARGUMENTS"
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S
LAMBDA_IS_NOT_LAST_PARAMETER(false, "lambda inside function parameters should be in the end"),
TOO_MANY_PARAMETERS(false, "function has too many parameters"),
NESTED_BLOCK(false, "function has too many nested blocks and should be simplified"),
WRONG_OVERLOADING_FUNCTION_ARGUMENTS(false, "use default argument instead of function overloading"),
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy
::AccurateCalculationsRule,
::LineLength,
::TypeAliasRule,
::OverloadingArgumentsFunction,
::FunctionLength,
::LambdaParameterOrder,
::FunctionArgumentsSize,
Expand Down
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)
}
}
}
6 changes: 5 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,8 @@
- name: NESTED_BLOCK
enabled: true
configuration:
maxNestedBlockQuantity: '4'
maxNestedBlockQuantity: '4'
# Checks that function use default values, instead overloading
- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS
enabled: true
configuration: {}
6 changes: 5 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,8 @@
- name: NESTED_BLOCK
enabled: true
configuration:
maxNestedBlockQuantity: '4'
maxNestedBlockQuantity: '4'
# Checks that function use default values, instead overloading
- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS
enabled: true
configuration: {}
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()
)
}
}
2 changes: 1 addition & 1 deletion diktat-ruleset/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion diktat-test-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
3 changes: 2 additions & 1 deletion info/available-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@
| 5 | 5.1.2 | NESTED_BLOCK | Check if function has more nested blocks than expected | no | maxNestedBlockQuantity
| 5 | 5.1.3 | AVOID_NESTED_FUNCTIONS | Check: if there are any nested functions<br>Fix: declare function in the outer scope | yes | - | + |
| 5 | 5.2.1 | LAMBDA_IS_NOT_LAST_PARAMETER | Checks that lambda inside function parameters isn't in the end | no | - |
| 5 | 5.2.2 | TOO_MANY_PARAMETERS | Check: if function contains more parameters than allowed | no | maxParameterListSize |
| 5 | 5.2.2 | TOO_MANY_PARAMETERS | Check: if function contains more parameters than allowed | no | maxParameterListSize |
| 5 | 5.2.3 | WRONG_OVERLOADING_FUNCTION_ARGUMENTS | Check: function has overloading instead use default arguments | no | -|
Loading

0 comments on commit f24fea1

Please sign in to comment.