In the analysis api, is there any equivalent of `E...
# k2-adopters
a
In the analysis api, is there any equivalent of
Errors.NAME_SHADOWING
using
public fun KtElement.diagnostics(filter: KaDiagnosticCheckerFilter): Collection<KaDiagnosticWithPsi<*>>
?
r
Hi! Please describe your use-case 🙏
a
It was being used in the detekt
NoNameShadowing
rule which warns users that particular varaible is being shadowed. Usage pattern is like below
Copy code
override fun visitProperty(property: KtProperty) {
        super.visitProperty(property)
        checkNameShadowing(property)
    }

    override fun visitDestructuringDeclarationEntry(multiDeclarationEntry: KtDestructuringDeclarationEntry) {
        super.visitDestructuringDeclarationEntry(multiDeclarationEntry)
        checkNameShadowing(multiDeclarationEntry)
    }

    override fun visitParameter(parameter: KtParameter) {
        super.visitParameter(parameter)
        checkNameShadowing(parameter)
    }

    private fun checkNameShadowing(declaration: KtNamedDeclaration) {
        val nameIdentifier = declaration.nameIdentifier ?: return
        if (bindingContext.diagnostics.forElement(declaration).any { it.factory == Errors.NAME_SHADOWING }) {
            report(Finding(Entity.from(nameIdentifier), "Name shadowed: ${nameIdentifier.text}"))
        }
    }