From 6255584ff82694acb18ee7ecfd373bbe896e3468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Thu, 4 Aug 2022 16:31:15 +0200 Subject: [PATCH] Improve documentation --- .../detekt/api/internal/RequiresTypeResolution.kt | 3 +++ website/docs/gettingstarted/type-resolution.md | 14 ++------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/RequiresTypeResolution.kt b/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/RequiresTypeResolution.kt index ba5037c4dfe..0d9d523891a 100644 --- a/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/RequiresTypeResolution.kt +++ b/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/RequiresTypeResolution.kt @@ -2,6 +2,9 @@ package io.gitlab.arturbosch.detekt.api.internal /** * Annotated [io.gitlab.arturbosch.detekt.api.Rule] requires type resolution to work. + * + * The detekt core will honor this annotation and it will not run any rule with this annotation if the bindingContext + * is empty. */ @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.RUNTIME) diff --git a/website/docs/gettingstarted/type-resolution.md b/website/docs/gettingstarted/type-resolution.md index 295f4a47a16..86d3c5cfbe1 100644 --- a/website/docs/gettingstarted/type-resolution.md +++ b/website/docs/gettingstarted/type-resolution.md @@ -100,19 +100,9 @@ Rules that are using type resolution, access the [bindingContext](https://github By default, the `bindingContext` is initialized as `BindingContext.EMPTY`. This is the **default value** that the rule receives if type resolution is **not enabled**. -Therefore, is generally advised to wrap your rules with a check for an empty binding context ([source](https://github.com/detekt/detekt/blob/cd659ce8737fb177caf140f46f73a1a86b22be56/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckNotNull.kt#L37-L39)): +Therefore, is generally advised to annotate your `Rule` with `@RequiresTypeResolution` to ensure that your rule doesn't run if you don't have a proper `BindingContext`. -```kotlin - override fun visitCallExpression(expression: KtCallExpression) { - super.visitCallExpression(expression) - - if (bindingContext == BindingContext.EMPTY) return - - // Rest of the rule that will run only with type resolution enabled. - } -``` - -If the `bindingContext` is not `EMPTY`, you are free to use it to resolve types and get access to all the information needed for your rules. As a rule of thumb, we recommend to get inspiration from other rules on how they're using the `bindingContext`. +If your rule is annotated with `@RequiresTypeResolution` you are free to use it to resolve types and get access to all the information needed for your rules. As a rule of thumb, we recommend to get inspiration from other rules on how they're using the `bindingContext`. ## Testing a rule that uses type resolution