diff --git a/kotlin-checks-test-sources/src/main/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheckSample.kt b/kotlin-checks-test-sources/src/main/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheckSample.kt index 507a7ca12..f75444b0a 100644 --- a/kotlin-checks-test-sources/src/main/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheckSample.kt +++ b/kotlin-checks-test-sources/src/main/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheckSample.kt @@ -2,7 +2,7 @@ package checks class SimplifyFilteringBeforeTerminalOperationCheckSample { fun test(list: List, set: Set, array: Array) { - list.filter { it > 5 }.any() // Noncompliant {{Remove "filter { it > 5 }" and replace "any()" with "any { it > 5 }".}} + list.filter { it > 5 }.any() // Noncompliant {{Remove `filter { it > 5 }` and replace `any()` with `any { it > 5 }`.}} // ^^^^^^^^^^^^^^^^^ list.any { it > 5 } @@ -40,11 +40,11 @@ class SimplifyFilteringBeforeTerminalOperationCheckSample { list.map { it + 1 }.filter { it < 10 }.filter { it > 5 }.map { it }.any() with(list) { - filter { it > 5 }.any() // Noncompliant {{Remove "filter { it > 5 }" and replace "any()" with "any { it > 5 }".}} + filter { it > 5 }.any() // Noncompliant {{Remove `filter { it > 5 }` and replace `any()` with `any { it > 5 }`.}} // ^^^^^^^^^^^^^^^^^ } - (list.filter { it > 5 }).any() // Noncompliant {{Remove "filter { it > 5 }" and replace "any()" with "any { it > 5 }".}} + (list.filter { it > 5 }).any() // Noncompliant {{Remove `filter { it > 5 }` and replace `any()` with `any { it > 5 }`.}} // ^^^^^^^^^^^^^^^^^ list.any() diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheck.kt b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheck.kt index 32c539084..761501003 100644 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheck.kt +++ b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/SimplifyFilteringBeforeTerminalOperationCheck.kt @@ -28,6 +28,7 @@ import org.sonar.check.Rule import org.sonarsource.kotlin.api.checks.CallAbstractCheck import org.sonarsource.kotlin.api.checks.FunMatcher import org.sonarsource.kotlin.api.frontend.KotlinFileContext +import org.sonarsource.kotlin.api.reporting.message private const val KOTLIN_COLLECTIONS_QUALIFIER = "kotlin.collections" private val FILTER_MATCHER = FunMatcher(qualifier = KOTLIN_COLLECTIONS_QUALIFIER, name = "filter") { withArguments("kotlin.Function1") } @@ -54,7 +55,15 @@ class SimplifyFilteringBeforeTerminalOperationCheck : CallAbstractCheck() { val terminalOpCallText = callExpression.text val terminalOpWithPredicate = "${callExpression.calleeExpression!!.text} $filterPredicateText" - val message = "Remove \"$filterCallText\" and replace \"$terminalOpCallText\" with \"$terminalOpWithPredicate\"." + val message = message { + +"Remove " + code(filterCallText) + +" and replace " + code(terminalOpCallText) + +" with " + code(terminalOpWithPredicate) + +"." + } kotlinFileContext.reportIssue(filterCallBeforeTerminalOp.callElement, message) }