Skip to content

Commit

Permalink
Warn about enabled rules that are not going to run
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Sep 10, 2022
1 parent cc29248 commit 6708184
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Expand Up @@ -52,6 +52,9 @@ internal class Analyzer(
} else {
runSync(ktFiles, bindingContext, compilerResources)
}
if (bindingContext == BindingContext.EMPTY) {
warnAboutEnabledRequiresTypeResolutionRules(settings::info)
}

val findingsPerRuleSet = HashMap<RuleSetId, List<Finding>>()
for (findings in findingsPerFile) {
Expand Down Expand Up @@ -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<RequiresTypeResolution>() }
.forEach { rule ->
log("The rule '${rule.ruleId}' requires type resolution but it was run without it.")
}
}
}

private fun filterSuppressedFindings(rule: BaseRule, bindingContext: BindingContext): List<Finding> {
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6708184

Please sign in to comment.