How to get rid of from SpreadOperator performance warning that was given by Detekt while using Spring Boot?

Viewed 3777

Recently, I added Detekt analyzer to my application.

After I runned detekt (./gradlew detekt), I got SpreadOperator warning in my main class of application.

Warning on code: runApplication<MessCallsApplication>(*args)

You can read about SpreadOperator warning here: SpreadOperator Warning

my main class:

@SpringBootApplication(exclude = [RedisAutoConfiguration::class])
@EnableCircuitBreaker
@EnableScheduling
class MyApplication {

    companion object : KLogging()
}

fun main(args: Array<String>) {
    Thread.setDefaultUncaughtExceptionHandler { _, exception ->
        MessCallsApplication.logger.error(exception) { "Uncaught exception" }
    }

    runApplication<MessCallsApplication>(*args)
}

Question is, What is the best practice to get rid of from that SpreadOperator warning? Or is it impossible?

4 Answers

You can add @Suppress("SpreadOperator") before your expression like this:

@SpringBootApplication(exclude = [RedisAutoConfiguration::class])
@EnableCircuitBreaker
@EnableScheduling
class MyApplication {

    companion object : KLogging()
}

fun main(args: Array<String>) {
    Thread.setDefaultUncaughtExceptionHandler { _, exception ->
        MessCallsApplication.logger.error(exception) { "Uncaught exception" }
    }
    @Suppress("SpreadOperator")
    runApplication<MessCallsApplication>(*args)
}

In your particular case:

 runApplication<MessCallsApplication>(args = args)

Edit:

This detekt warning is not longer a case: https://github.com/detekt/detekt/pull/3157

However as Klitos Kyriacou mentioned in code - this array is copied (even twice!). Decompiled bytecode:

public final class MessCallsApplicationKt {
   public static final void main(@NotNull String[] args) {
      Intrinsics.checkNotNullParameter(args, "args");
      Schedulers.enableMetrics();
      String[] args$iv = (String[])Arrays.copyOf(args, args.length);
      int $i$f$runApplication = false;
      Intrinsics.checkExpressionValueIsNotNull(SpringApplication.run(MessCallsApplication.class, (String[])Arrays.copyOf(args$iv, args$iv.length)), "SpringApplication.run(T::class.java, *args)");
   }
}

or simply change from:

fun main(args: Array<String>)

to

fun main(vararg args: String)

you have to pass the * to the run method:

fun main(vararg args: String) {
  runApplication<RateRefresherApplication>(*args)
}
Related