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 1 commit
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 @@ -5,8 +5,11 @@ 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") {

Expand All @@ -26,14 +29,14 @@ class AvoidEmptyPrimaryConstructor(private val configRules: List<RulesConfig>) :

@Suppress("UnsafeCallOnNullableType")
private fun checkCLass(node: KtClass) {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
if(node.primaryConstructor?.valueParameters?.isNotEmpty() != false)
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 {
//fixme should check if it possible to remove secondary constructor
warnOrFixOnEmptyPrimaryConstructor(node.node, false) {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EmptyPrimaryConstructorWarnTest: LintTestBase(::AvoidEmptyPrimaryConstruct
|
|class Some1() {
| val a = 10
| companion object() {}
| companion object {}
|}
|
|class Some2 {
Expand All @@ -35,6 +35,10 @@ class EmptyPrimaryConstructorWarnTest: LintTestBase(::AvoidEmptyPrimaryConstruct
| 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)
Expand Down