Skip to content

Commit

Permalink
Simplify FirSupertypeInitializedWithoutPrimaryConstructor checker
Browse files Browse the repository at this point in the history
  • Loading branch information
mglukhikh committed Nov 26, 2020
1 parent bf2b318 commit 97c1a3f
Showing 1 changed file with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

package org.jetbrains.kotlin.fir.analysis.checkers.declaration

import com.intellij.lang.LighterASTNode
import com.intellij.util.diff.FlyweightCapableTreeStructure
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.analysis.diagnostics.findChildByType
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
Expand All @@ -24,21 +21,16 @@ object FirSupertypeInitializedWithoutPrimaryConstructor : FirMemberDeclarationCh
return
}

val hasSupertypeWithConstructor = declaration.source?.anySupertypeHasConstructorParentheses() == true
val hasPrimaryConstructor = declaration.declarations.any { it is FirConstructor && it.isPrimary }

if (hasSupertypeWithConstructor && !hasPrimaryConstructor) {
reporter.report(declaration.source)
if (declaration.declarations.any { it is FirConstructor && it.isPrimary }) {
return
}
}

private fun FirSourceElement.anySupertypeHasConstructorParentheses(): Boolean {
return lighterASTNode.anySupertypeHasConstructorParentheses(treeStructure)
}

private fun LighterASTNode.anySupertypeHasConstructorParentheses(tree: FlyweightCapableTreeStructure<LighterASTNode>): Boolean {
val superTypes = tree.findChildByType(this, KtNodeTypes.SUPER_TYPE_LIST) ?: return false
return tree.findChildByType(superTypes, KtNodeTypes.SUPER_TYPE_CALL_ENTRY) != null
for (superTypeRef in declaration.superTypeRefs) {
val source = superTypeRef.source ?: continue
if (source.treeStructure.getParent(source.lighterASTNode)?.tokenType == KtNodeTypes.CONSTRUCTOR_CALLEE) {
reporter.report(declaration.source)
}
}
}

private fun DiagnosticReporter.report(source: FirSourceElement?) {
Expand Down

0 comments on commit 97c1a3f

Please sign in to comment.