From 5877c4fea5558c902b0e2747bdf5215edc462c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Tue, 16 Aug 2022 21:18:08 +0200 Subject: [PATCH] Warn about enabled rules that are not going to run --- .../io/gitlab/arturbosch/detekt/core/Analyzer.kt | 16 ++++++++++++++++ .../arturbosch/detekt/core/AnalyzerSpec.kt | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/Analyzer.kt b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/Analyzer.kt index d469dadcf86..2be31a21c3e 100644 --- a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/Analyzer.kt +++ b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/Analyzer.kt @@ -53,6 +53,9 @@ internal class Analyzer( } else { runSync(ktFiles, bindingContext, compilerResources) } + if (bindingContext == BindingContext.EMPTY) { + warnAboutEnabledRequiresTypeResolutionRules(settings::info) + } val findingsPerRuleSet = HashMap>() for (findings in findingsPerFile) { @@ -140,6 +143,19 @@ internal class Analyzer( return result } + + private fun warnAboutEnabledRequiresTypeResolutionRules(log: (String) -> Unit) { + providers.asSequence() + .map { it to config.subConfig(it.ruleSetId) } + .filter { (_, ruleSetConfig) -> ruleSetConfig.isActive() } + .map { (provider, ruleSetConfig) -> provider.instance(ruleSetConfig) to ruleSetConfig } + .flatMap { (ruleSet, _) -> ruleSet.rules.asSequence() } + .filter { rule -> (rule as? Rule)?.active == true } + .filter { rule -> rule::class.hasAnnotation() } + .forEach { rule -> + log("The rule '${rule.ruleId}' requires type resolution but it was run without it.") + } + } } private fun filterSuppressedFindings(rule: BaseRule, bindingContext: BindingContext): List { diff --git a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt index 5ed0993ef36..952149dfcca 100644 --- a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt +++ b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt @@ -74,7 +74,9 @@ class AnalyzerSpec(val env: KotlinCoreEnvironment) { val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList()) assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }.values.flatten()).isEmpty() - assertThat(output.toString()).isEmpty() + assertThat(output.toString()).isEqualTo( + "The rule 'RequiresTypeResolutionMaxLineLength' requires type resolution but it was run without it.\n" + ) } @Test @@ -89,7 +91,9 @@ class AnalyzerSpec(val env: KotlinCoreEnvironment) { val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider(30)), emptyList()) assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }.values.flatten()).hasSize(1) - assertThat(output.toString()).isEmpty() + assertThat(output.toString()).isEqualTo( + "The rule 'RequiresTypeResolutionMaxLineLength' requires type resolution but it was run without it.\n" + ) } @Test