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

Interface vs Data class bug#1003 #1014

Merged
merged 2 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.pinterest.ktlint.core.ast.ElementType.DATA_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.ENUM_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.INNER_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.INTERFACE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.core.ast.ElementType.OPEN_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR
Expand Down Expand Up @@ -42,10 +43,11 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
}

private fun handleClass(node: ASTNode) {
if (node.isDataClass()) {
return
with((node.psi as KtClass)) {
if (isData() || isInterface()) {
return
}
}

if (node.canBeDataClass()) {
raiseWarn(node)
}
Expand Down Expand Up @@ -143,15 +145,6 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
return true
}

@Suppress("UnsafeCallOnNullableType")
private fun ASTNode.isDataClass(): Boolean {
if (hasChildOfType(MODIFIER_LIST)) {
val list = getFirstChildWithType(MODIFIER_LIST)!!
return list.getChildren(null).any { it.elementType == DATA_KEYWORD }
}
return false
}

companion object {
private val badModifiers = listOf(OPEN_KEYWORD, ABSTRACT_KEYWORD, INNER_KEYWORD, SEALED_KEYWORD, ENUM_KEYWORD)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,18 @@ class DataClassesRuleWarnTest : LintTestBase(::DataClassesRule) {
""".trimMargin()
)
}

@Test
@Tag(USE_DATA_CLASS)
fun `shouldn't trigger on interface`() {
lintMethod(
"""
|interface Credentials {
| val code: String
| val success: Boolean
| val message: String
|}
""".trimMargin()
)
}
}