Skip to content

Commit

Permalink
Apply Kotlin inspection "Variable declaration could be moved inside '…
Browse files Browse the repository at this point in the history
…when'"

GitOrigin-RevId: 5c55b548b940f7bfba46f353d4ced63f33ec5f47
  • Loading branch information
abelkov authored and intellij-monorepo-bot committed Jun 1, 2023
1 parent fc9b5f6 commit 73e6691
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class WiFiPairingControllerImpl(private val project: Project,

// Check ADB is valid and mDNS is supported on this platform
project.coroutineScope.launch(uiThread(ModalityState.any())) {
val supportState = pairingService.checkMdnsSupport()
when (supportState) {
when (pairingService.checkMdnsSupport()) {
MdnsSupportState.Supported -> {
view.showMdnsCheckSuccess()
qrCodeScanningController.startPairingProcess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ object MaterialIconsUtils {
* This should be used whenever we want to read or write material icons, since we rely on this consistency.
*/
fun getIconFileNameWithoutExtension(iconName: String, styleName: String): String {
val family = styleName.toDirFormat().substringAfter("materialicons")
val familyPrefix = when (family) {
val familyPrefix = when (val family = styleName.toDirFormat().substringAfter("materialicons")) {
"" -> "baseline"
"outlined" -> "outline"
else -> family
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,8 @@ class AndroidWatchFaceConfigurationExecutorTest : AndroidConfigurationExecutorBa

deviceState.setActivityManager { args: List<String>, serviceOutput: ServiceOutput ->
val wholeCommand = args.joinToString(" ")

when (wholeCommand) {
checkVersion -> serviceOutput.writeStdout("Broadcast completed: result=1, data=\"3\"")
if (wholeCommand == checkVersion) {
serviceOutput.writeStdout("Broadcast completed: result=1, data=\"3\"")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ class StartReattachingDebuggerTest {

deviceState.setActivityManager { args: List<String>, serviceOutput: ServiceOutput ->
val wholeCommand = args.joinToString(" ")


when (wholeCommand) {
"force-stop $MASTER_PROCESS_NAME" -> latch.countDown()
"force-stop $APP_ID" -> latch.countDown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ class StabilityInferencer(val context: IrPluginContext) {
): Stability {
// if isEnum, return true
// class hasStableAnnotation()
val owner = classifier.owner
return when (owner) {
return when (val owner = classifier.owner) {
is IrClass -> stabilityOf(owner, substitutions, currentlyAnalyzing)
is IrTypeParameter -> Stability.Unstable
else -> error("Unexpected IrClassifier: $owner")
Expand Down Expand Up @@ -448,8 +447,7 @@ class StabilityInferencer(val context: IrPluginContext) {
val fqName = function.fqNameForIrSerialization

val baseStability = stabilityOf(expr.type)
val mask = stableProducingFunctions[fqName.asString()]
return when (mask) {
return when (val mask = stableProducingFunctions[fqName.asString()]) {
null -> baseStability
0 -> Stability.Stable
else -> Stability.Combined(
Expand Down Expand Up @@ -508,14 +506,11 @@ private fun IrType.getInlinedClass(): IrClass? {
return erased
}

// From Kotin's InlineClasses.kt
private tailrec fun erase(type: IrType): IrClass? {
val classifier = type.classifierOrFail

return when (classifier) {
is IrClassSymbol -> classifier.owner
is IrScriptSymbol -> null // TODO: check if correct
is IrTypeParameterSymbol -> erase(classifier.owner.superTypes.first())
else -> error(classifier)
}
// From Kotlin's InlineClasses.kt
private tailrec fun erase(type: IrType): IrClass? =
when (val classifier = type.classifierOrFail) {
is IrClassSymbol -> classifier.owner
is IrScriptSymbol -> null // TODO: check if correct
is IrTypeParameterSymbol -> erase(classifier.owner.superTypes.first())
else -> error(classifier)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,7 @@ abstract class AbstractComposeLowering(
is IrConstructorCall -> isStatic()
is IrCall -> isStatic()
is IrGetValue -> {
val owner = symbol.owner
when (owner) {
when (val owner = symbol.owner) {
is IrVariable -> {
// If we have an immutable variable whose initializer is also static,
// then we can determine that the variable reference is also static.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2578,8 +2578,7 @@ class ComposableFunctionBodyTransformer(
when {
arg.isStatic() -> meta.isStatic = true
arg is IrGetValue -> {
val owner = arg.symbol.owner
when (owner) {
when (val owner = arg.symbol.owner) {
is IrValueParameter -> {
extractParamMetaFromScopes(meta, owner)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,7 @@ class IrSourcePrinterVisitor(
val rhsStatement = expression.statements[1]
val rhs = when (rhsStatement) {
is IrBlock -> {
val target = rhsStatement.statements[1]
when (target) {
when (val target = rhsStatement.statements[1]) {
is IrVariable -> target.initializer
else -> target
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ class KotlinDslParser(
expression: KtDotQualifiedExpression
) : GradleDslExpression {
val receiver = expression.receiverExpression
val selector = expression.selectorExpression
when (selector) {
when (val selector = expression.selectorExpression) {
is KtCallExpression -> {
// Check if this is about a localMethod used for blocks referencing, or not.
val referenceName = selector.name()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@ internal fun methodCallBlockName(expression: KtCallExpression): String? {
// TODO(xof): we should handle injections / resolving here:
// buildTypes.getByName("$foo") { ... }
// buildTypes.getByName(foo) { ... }
val argument = arguments[0].getArgumentExpression()
return when (argument) {
return when (val argument = arguments[0].getArgumentExpression()) {
is KtStringTemplateExpression -> argument.literalContents()
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,8 @@ class KotlinDslWriter(override val internalContext: BuildModelContext) : KotlinD
}

if (psiExpression != null) {
val replace = psiExpression.replace(newLiteral)
// Make sure we replaced with the right psi element for the GradleDslLiteral.
when (replace) {
when (val replace = psiExpression.replace(newLiteral)) {
is KtStringTemplateExpression, is KtConstantExpression, is KtNameReferenceExpression, is KtDotQualifiedExpression,
is KtArrayAccessExpression, is KtBinaryExpressionWithTypeRHS -> literal.setExpression(replace)
else -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ object SchedParser : FunctionHandlerRegistry() {
}

private fun PreviewReader.readSchedulingState(): SchedulingState {
val byte = readByte()
return when (byte) {
return when (readByte()) {
'S'.code.toByte() -> SchedulingState.SLEEPING
'R'.code.toByte() -> SchedulingState.RUNNABLE
'D'.code.toByte() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class PsModuleTypeTest : DependencyTestCase() {
}

private fun moduleWithoutSyncedModel(project: PsProject, name: String): PsModule {
val moduleWithSyncedModel = project.findModuleByName(name)
return when (moduleWithSyncedModel) {
return when (val moduleWithSyncedModel = project.findModuleByName(name)) {
is PsAndroidModule -> PsAndroidModule(project, moduleWithSyncedModel.gradlePath).apply {
init(moduleWithSyncedModel.name, null, null, null, null, moduleWithSyncedModel.parsedModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ private fun ProjectDumper.dump(facet: Facet<*>) {
nest {
prop("TypeId") { facet.typeId.toString() }
prop("ExternalSource") { facet.externalSource?.id }
val configuration = facet.configuration
when (configuration) {
when (val configuration = facet.configuration) {
is GradleFacetConfiguration -> dump(configuration)
is AndroidFacetConfiguration -> dump(configuration)
is NdkFacetConfiguration -> dump(configuration)
Expand Down

0 comments on commit 73e6691

Please sign in to comment.