Simplify FirSupertypeInitializedWithoutPrimaryConstructor checker

This commit is contained in:
Mikhail Glukhikh
2020-11-24 11:41:32 +03:00
parent bf2b318bee
commit 97c1a3f270

View File

@@ -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
@@ -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?) {