Skip to content

NoSuchElementException after updating to 1.20.0-RC1 #4604

Description

@eygraber

Just tried out 1.20.0-RC1 and got a few of these exceptions:

java.lang.IllegalStateException: Analyzing /home/eli/workspace/myapp/app/src/main/kotlin/co/app/common/AppInfo.kt led to an exception. 
  The original exception message was: java.util.NoSuchElementException: Sequence contains no element matching the predicate.
  Running detekt '1.20.0-RC1' on Java '17.0.2+8-LTS' on OS 'Linux'

Here is AppInfo.kt:

package co.app.common

import co.app.coroutines.flow.conflatedSharedFlow
import kotlinx.coroutines.flow.first

interface AppInfo {
  val versionName: String
  val versionCode: Int

  val playStorePackage: String

  val osVersion: Int

  val appVersion: String

  var suggestedVersion: String?
  suspend fun awaitSuggestedVersionChanged(): String

  fun isAppBelowSuggestedVersion(suggestedVersion: String?): Boolean
}

internal class AppInfoImpl(
  appVersionName: String,
  appVersionCode: Int,
  appPackageName: String,
  currentOsVersion: Int
) : AppInfo {
  override val versionName = appVersionName

  override val versionCode = appVersionCode

  override val playStorePackage = appPackageName

  override val osVersion = currentOsVersion

  override val appVersion = "a${versionName.removeSuffix("-DEBUG")}.0"

  private val suggestedVersionFlow = conflatedSharedFlow<String>()

  override var suggestedVersion: String? = null
    set(value) {
      field = value
      if(value != null) {
        suggestedVersionFlow.tryEmit(value)
      }
    }

  override suspend fun awaitSuggestedVersionChanged(): String = suggestedVersionFlow.first()

  override fun isAppBelowSuggestedVersion(suggestedVersion: String?): Boolean {
    tailrec fun isAppBelowSuggested(current: List<Int>, suggested: List<Int>, index: Int): Boolean =
      when {
        index >= current.size -> false

        current[index] != suggested[index] -> current[index] < suggested[index]

        else -> isAppBelowSuggested(current, suggested, index + 1)
      }

    return suggestedVersion?.let {
      val currentVersion = versionName.removeSuffix("-DEBUG")

      when(suggestedVersion) {
        currentVersion -> false

        else -> {
          val currentVersionSplit = currentVersion.split(".").mapNotNull { it.toIntOrNull() }
          val suggestedVersionSplit = suggestedVersion.split(".").mapNotNull { it.toIntOrNull() }

          when {
            currentVersionSplit.size != 3 || suggestedVersionSplit.size != 3 -> false

            else -> isAppBelowSuggested(currentVersionSplit, suggestedVersionSplit, index = 0)
          }
        }
      }
    } ?: false
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions