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

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][2]

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?



Solution 1:[1]

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)
}

Solution 2:[2]

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)");
   }
}

Solution 3:[3]

or simply change from:

fun main(args: Array<String>)

to

fun main(vararg args: String)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akobor
Solution 2
Solution 3 Jeff Fang