Skip to content

Commit

Permalink
Code highlighting in reported issue message for rule S6527 (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
ADarko22 authored Nov 21, 2023
1 parent 059863e commit e664285
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package checks

class SimplifyFilteringBeforeTerminalOperationCheckSample {
fun test(list: List<Int>, set: Set<Int>, array: Array<Int>) {
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 }

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand All @@ -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)
}
Expand Down

0 comments on commit e664285

Please sign in to comment.