Skip to content

KeySetAbsentAssertion

Issue ID KeySetAbsentAssertion · Severity Warning · Category Usability · Default on

Flags assertThat(map.keys).doesNotContain(k) — checking key absence by materialising the key set. Use Assert<Map<K, V>>.doesNotContainKey(k) directly.

Why

The Iterable.doesNotContain form produces a generic message about a Set<K>. doesNotContainKey is purpose-built for the same intent, names the map in the failure, and reads more clearly at the call site.

Example

import assertk.assertThat
import assertk.assertions.doesNotContain

fun keyMissing() {
    val map = mapOf("9A3E6FAC" to "John")
    assertThat(map.keys).doesNotContain("missing")
}
import assertk.assertThat
import assertk.assertions.doesNotContainKey

fun keyMissing() {
    val map = mapOf("9A3E6FAC" to "John")
    assertThat(map).doesNotContainKey("missing")
}

Quick fix

Yes — the IDE rewrites assertThat(map.keys).doesNotContain(k) to assertThat(map).doesNotContainKey(k) and adds assertk.assertions.doesNotContainKey.

Source

MapAssertionDetector.kt