Add a detection for "throw error(...)"

In Kotlin, the error() function is a part of the standard library, and it is specifically designed to throw an exception. When you call error(message: String), it internally throws an instance of IllegalStateException with the given message.

Therefore, you do not need to use throw with error(), as it already handles throwing the exception. Using throw in front of error() would be redundant and may lead to confusion.

Using (snippet of Noncompliant Code):

throw error("fatal: this is an error")

is unnecessary and incorrect because error() itself throws an exception.

Here’s the correct way to use it (snippet of Compilant Code):

error("fatal: this is an error")

This will immediately throw an IllegalStateException with the message "fatal: this is an error".

Hi @jmini

Thanks for the suggestion, Indeed throwing error doesn’t really make sense.
This is only possible because the return type of the function error() is kotlin.Nothing, so compiler doesn’t complain.

We’ll think if the rule makes more sense in the error() function context or with any function returning Nothing.

Best,
Margarita