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

Rule 6.1.3 Do not use the primary constructor if it is empty and has no sense #528

Merged
merged 7 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,7 @@
enabled: true
# Checks if extension function with the same signature don't have related classes
- name: EXTENSION_FUNCTION_SAME_SIGNATURE
enabled: true
# Checks if there is empty primary constructor
- name: EMPTY_PRIMARY_CONSTRUCTOR
enabled: true
8 changes: 5 additions & 3 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ public object WarningNames {

public const val CLASS_SHOULD_NOT_BE_ABSTRACT: String = "CLASS_SHOULD_NOT_BE_ABSTRACT"

public const val TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED: String =
"TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED"

public const val CUSTOM_GETTERS_SETTERS: String = "CUSTOM_GETTERS_SETTERS"

public const val COMPACT_OBJECT_INITIALIZATION: String = "COMPACT_OBJECT_INITIALIZATION"

public const val USELESS_SUPERTYPE: String = "USELESS_SUPERTYPE"

public const val TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED: String =
"TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED"

public const val EXTENSION_FUNCTION_SAME_SIGNATURE: String = "EXTENSION_FUNCTION_SAME_SIGNATURE"

public const val EMPTY_PRIMARY_CONSTRUCTOR: String = "EMPTY_PRIMARY_CONSTRUCTOR"
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S
USELESS_SUPERTYPE(true,"unnecessary supertype specification"),
TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED(true, "trivial property accessors are not recommended"),
EXTENSION_FUNCTION_SAME_SIGNATURE(false, "extension functions should not have same signature if their receiver classes are related"),
EMPTY_PRIMARY_CONSTRUCTOR(true, "avoid empty primary constructor"),
;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.cqfn.diktat.ruleset.rules

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.CLASS
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_PRIMARY_CONSTRUCTOR
import org.cqfn.diktat.ruleset.utils.getIdentifierName
import org.cqfn.diktat.ruleset.utils.prettyPrint
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.psiUtil.isPrivate
import org.jetbrains.kotlin.psi.psiUtil.isPublic

class AvoidEmptyPrimaryConstructor(private val configRules: List<RulesConfig>) : Rule("avoid-empty-primary-constructor") {


private var isFixMode: Boolean = false
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit)

override fun visit(node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
emitWarn = emit
isFixMode = autoCorrect

if (node.elementType == CLASS)
checkCLass(node.psi as KtClass)
}

@Suppress("UnsafeCallOnNullableType")
private fun checkCLass(node: KtClass) {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
println(node.node.prettyPrint())
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
if(node.primaryConstructor?.valueParameters?.isNotEmpty() != false || node.primaryConstructor?.isPrivate() == true)
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
return
if (node.secondaryConstructors.isEmpty()) {
warnOrFixOnEmptyPrimaryConstructor(node.node, true) {
node.node.removeChild(node.primaryConstructor!!.node)
}
} else {
warnOrFixOnEmptyPrimaryConstructor(node.node, false) {}
}
}

@Suppress("UnsafeCallOnNullableType")
private fun warnOrFixOnEmptyPrimaryConstructor(classNode: ASTNode, isFix: Boolean, autofix: () -> Unit) {
EMPTY_PRIMARY_CONSTRUCTOR.warnAndFix(configRules, emitWarn, isFix, classNode.getIdentifierName()!!.text,
classNode.startOffset, classNode){
autofix()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy
::BracesInConditionalsAndLoopsRule,
::BlockStructureBraces,
::EmptyBlock,
::AvoidEmptyPrimaryConstructor,
::EnumsSeparated,
::SingleLineStatementsRule,
::MultipleModifiersSequence,
Expand Down
3 changes: 3 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,7 @@
enabled: true
# Checks if extension function with the same signature don't have related classes
- name: EXTENSION_FUNCTION_SAME_SIGNATURE
enabled: true
# Checks if there is empty primary constructor
- name: EMPTY_PRIMARY_CONSTRUCTOR
enabled: true
3 changes: 3 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,7 @@
enabled: true
# Checks if extension function with the same signature don't have related classes
- name: EXTENSION_FUNCTION_SAME_SIGNATURE
enabled: true
# Checks if there is empty primary constructor
- name: EMPTY_PRIMARY_CONSTRUCTOR
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cqfn.diktat.ruleset.chapter6

import generated.WarningNames
import org.cqfn.diktat.ruleset.rules.AvoidEmptyPrimaryConstructor
import org.cqfn.diktat.util.FixTestBase
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

class EmptyPrimaryConstructorFixTest: FixTestBase("test/chapter6/primary_constructor", ::AvoidEmptyPrimaryConstructor) {

@Test
@Tag(WarningNames.EMPTY_PRIMARY_CONSTRUCTOR)
fun `should remove empty primary constructor`() {
fixAndCompare("EmptyPCExpected.kt", "EmptyPCTest.kt")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.cqfn.diktat.ruleset.chapter6

import com.pinterest.ktlint.core.LintError
import generated.WarningNames
import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_PRIMARY_CONSTRUCTOR
import org.cqfn.diktat.ruleset.rules.AvoidEmptyPrimaryConstructor
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.util.LintTestBase
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

class EmptyPrimaryConstructorWarnTest: LintTestBase(::AvoidEmptyPrimaryConstructor) {
private val ruleId = "$DIKTAT_RULE_SET_ID:avoid-empty-primary-constructor"

@Test
@Tag(WarningNames.EMPTY_PRIMARY_CONSTRUCTOR)
fun `simple classes with empty primary constructor`() {
lintMethod(
"""
|class Some() {
| val a = 10
| constructor(a: String): this() {
| this.a = a
| }
|}
|
|class Some1() {
| val a = 10
| companion object {}
|}
|
|class Some2 {
| val a = 10
| constructor(a: String): this() {
| this.a = a
| }
|}
|
|class Some3 private constructor () {
|
|}
""".trimMargin(),
LintError(1,1,ruleId, "${EMPTY_PRIMARY_CONSTRUCTOR.warnText()} Some", true),
LintError(8,1,ruleId, "${EMPTY_PRIMARY_CONSTRUCTOR.warnText()} Some1", true)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test.chapter6.primary_constructor

class Test {
var a: Int = 0
var b: Int = 0
}

class Test() {
var a = "Property"

init {
println("some init")
}

constructor(a: String): this() {
this.a = a
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test.chapter6.primary_constructor

class Test() {
var a: Int = 0
var b: Int = 0
}

class Test() {
var a = "Property"

init {
println("some init")
}

constructor(a: String): this() {
this.a = a
}
}
1 change: 1 addition & 0 deletions info/available-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
| 5 | 5.2.3 | WRONG_OVERLOADING_FUNCTION_ARGUMENTS | Check: function has overloading instead use default arguments | no | -|
| 6 | 6.1.1 | SINGLE_CONSTRUCTOR_SHOULD_BE_PRIMARY | Check: warns if there is only one secondary constructor in a class<br>Fix: converts it to a primary constructor | yes | no | Support more complicated logic of constructor conversion |
| 6 | 6.1.2 | USE_DATA_CLASS | Check: if class can be made as data class | no | - | yes |
| 6 | 6.1.3 | EMPTY_PRIMARY_CONSTRUCTOR | Check: if there is empty primary constructor | yes| - | yes |
| 6 | 6.1.4 | MULTIPLE_INIT_BLOCKS | Checks that classes have only one init block | yes | no | - |
| 6 | 6.1.5 | USELESS_SUPERTYPE | Check: if override function can be removed | yes| - | |
| 6 | 6.1.6 | CLASS_SHOULD_NOT_BE_ABSTRACT | Checks: if abstract class has any abstract method. If not, warns that class should not be abstract<br>Fix: deletes abstract modifier | yes | - | - |
Expand Down