Skip to content

UseIndexAssertion

Issue ID UseIndexAssertion · Severity Warning · Category Correctness · Default on

Flags assertThat(collection[i]) and assertThat(array[i]) where assertk's index (and friends like first()) should be used.

Why

Indexing the collection inside assertThat throws an IndexOutOfBoundsException at the assertion site — the failure looks like a crash, not an assertion failure. Assert<List<T>>.index(i): Assert<T> asserts that the element exists and transforms the subject so you can chain further checks on the value.

The detector recognises both Array and java.util.List (and subtypes) as indexable. Reads through index that are not the assertThat argument — e.g. assertThat(array[1].sign).isEqualTo(1) — are intentionally not flagged.

Example

import assertk.assertThat
import assertk.assertions.isEqualTo

fun thirdElement() {
    val array = arrayOf(10, 100, 1_000)
    assertThat(array[2]).isEqualTo(1_000)
}
import assertk.assertThat
import assertk.assertions.index
import assertk.assertions.isEqualTo

fun thirdElement() {
    val array = arrayOf(10, 100, 1_000)
    assertThat(array).index(2).isEqualTo(1_000)
}

Quick fix

Yes — the IDE rewrites assertThat(array[i]) to assertThat(array).index(i) and adds the assertk.assertions.index import.

Source

IndexDetector.kt