A string literal is cast to a number type, or a number literal to String, which always fails

tx0/kotlin:literal-cast-to-other-typedefaultwarningreliability/api-misusedepth fastbug

What it finds

"42" as Int does not parse the string: as checks the type of the object and throws ClassCastException because a String is never an Int. toInt() converts.

From the rule's source, impossible_literal_cast.rs.

Example from the rule's tests
fun values(x: Any) {    val n = "42" as Inttx0 reports: as Int always throws ClassCastException on this literal    val s = 42 as Stringtx0 reports: as String always throws    val t = "42" as String    val u = x as Int    val v = "42" as? Int}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:literal-cast-to-other-type" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:literal-cast-to-other-type] reason="..."

tx0 explain tx0/kotlin:literal-cast-to-other-type shows the same in the terminal. Found a false positive? Report it.