KT.USE_REQUIRE

Use 'require()' instead of manually throwing an IllegalArgumentException

Kotlin provides a much more concise way to check preconditions than to manually throw an IllegalArgumentException.

Noncompliant Code

Copy
if (value == null) throw IllegalArgumentException("value should not be null")
if (value < 0) throw IllegalArgumentException("value is $value but should be at least 0")

Compliant Code

Copy
requireNotNull(value) { "value should not be null" }
require(value >= 0) { "value is $value but should be at least 0" }

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html