Skip to content

Commit deb897d

Browse files
authored
Fix various warnings and cleanup samples (#579)
* Fix various warnings and cleanup samples * Applied every IDE suggestion to the samples. We should keep the sample code copy-pasteable into the IDE without warnings. * Gradle mentions that compileOnly dependencies don't work in Wasm, so changed the dependency of `kotlinx-datetime-zoneinfo` from compileOnly to api. * Replaced the non-exhaustive-when error suppressions with an `as` cast. * Fixed various other warnings both in test/ and src/.
1 parent 5070925 commit deb897d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+135
-154
lines changed

buildSrc/src/main/kotlin/zoneInfosResourcesGenerator.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.gradle.api.file.Directory
12
import java.io.File
23

34
/*
@@ -51,7 +52,7 @@ private fun loadTzBinaries(
5152
}
5253
}
5354

54-
fun generateZoneInfosResources(zoneInfoDir: File, outputDir: File, version: String) {
55+
fun generateZoneInfosResources(zoneInfoDir: File, outputDir: Directory, version: String) {
5556
val header = buildString {
5657
appendLine()
5758
append("/* AUTOGENERATED FROM ZONE INFO DATABASE v.$version */")
@@ -70,7 +71,7 @@ fun generateZoneInfosResources(zoneInfoDir: File, outputDir: File, version: Stri
7071
loadedZones.forEachIndexed { id, tzData ->
7172
val tzDataName = "tzData$id"
7273
val data = generateByteArrayProperty(tzData, header, tzDataName)
73-
File(outputDir, "$tzDataName.kt").writeText(data)
74+
outputDir.file("$tzDataName.kt").asFile.writeText(data)
7475
tzData.fullTzNames.forEach { name ->
7576
zoneDataByNameBody.appendLine(" \"$name\" -> $tzDataName")
7677
getTimeZonesBody.appendLine(" \"$name\",")
@@ -97,5 +98,5 @@ fun generateZoneInfosResources(zoneInfoDir: File, outputDir: File, version: Stri
9798
append("}")
9899
}
99100

100-
File(outputDir, "tzData.kt").writeText(content)
101+
outputDir.file("tzData.kt").asFile.writeText(content)
101102
}

core/build.gradle.kts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import kotlinx.team.infra.mavenPublicationsPom
2-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3-
import java.net.URL
4-
import javax.xml.parsers.DocumentBuilderFactory
5-
import java.io.ByteArrayOutputStream
6-
import java.io.PrintWriter
72
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
3+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
84
import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
9-
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
5+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
106
import org.jetbrains.kotlin.konan.target.Family
7+
import java.io.ByteArrayOutputStream
8+
import java.io.PrintWriter
9+
import java.net.URL
10+
import javax.xml.parsers.DocumentBuilderFactory
1111

1212
plugins {
1313
kotlin("multiplatform")
@@ -22,10 +22,6 @@ mavenPublicationsPom {
2222
description.set("Kotlin Datetime Library")
2323
}
2424

25-
base {
26-
archivesBaseName = "kotlinx-datetime" // doesn't work
27-
}
28-
2925
val mainJavaToolchainVersion: String by project
3026
val modularJavaToolchainVersion: String by project
3127
val serializationVersion: String by project
@@ -112,6 +108,7 @@ kotlin {
112108
}
113109
}
114110

111+
@OptIn(ExperimentalWasmDsl::class)
115112
wasmJs {
116113
nodejs {
117114
testTask {
@@ -122,6 +119,7 @@ kotlin {
122119
}
123120
}
124121

122+
@OptIn(ExperimentalWasmDsl::class)
125123
wasmWasi {
126124
nodejs()
127125
}
@@ -295,7 +293,7 @@ tasks {
295293
// Workaround for https://youtrack.jetbrains.com/issue/KT-58303:
296294
// the `clean` task can't delete the expanded.lock file on Windows as it's still held by Gradle, failing the build
297295
val clean by existing(Delete::class) {
298-
setDelete(fileTree(buildDir) {
296+
setDelete(fileTree(layout.buildDirectory) {
299297
exclude("tmp/.cache/expanded/expanded.lock")
300298
})
301299
}
@@ -322,7 +320,7 @@ val downloadWindowsZonesMapping by tasks.registering {
322320
val output = "$projectDir/windows/src/internal/WindowsZoneNames.kt"
323321
outputs.file(output)
324322
doLast {
325-
val initialFileContents = try { File(output).readBytes() } catch(e: Throwable) { ByteArray(0) }
323+
val initialFileContents = try { File(output).readBytes() } catch(_: Throwable) { ByteArray(0) }
326324
val documentBuilderFactory = DocumentBuilderFactory.newInstance()
327325
// otherwise, parsing fails since it can't find the dtd
328326
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)

core/common/src/format/DateTimeComponents.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
538538
truncatedDate.year = requireParsedField(truncatedDate.year, "year") % 10_000
539539
val totalSeconds = try {
540540
val secDelta = safeMultiply((year!! / 10_000).toLong(), SECONDS_PER_10000_YEARS)
541-
val epochDays = truncatedDate.toLocalDate().toEpochDays().toLong()
541+
val epochDays = truncatedDate.toLocalDate().toEpochDays()
542542
safeAdd(secDelta, epochDays * SECONDS_PER_DAY + time.toSecondOfDay() - offset.totalSeconds)
543543
} catch (e: ArithmeticException) {
544544
throw DateTimeFormatException("The parsed date is outside the range representable by Instant", e)
@@ -654,10 +654,8 @@ internal class DateTimeComponentsFormat(override val actualFormat: CachedFormatS
654654
override fun timeZoneId() =
655655
actualBuilder.add(BasicFormatStructure(TimeZoneIdDirective()))
656656

657-
@Suppress("NO_ELSE_IN_WHEN")
658-
override fun dateTimeComponents(format: DateTimeFormat<DateTimeComponents>) = when (format) {
659-
is DateTimeComponentsFormat -> actualBuilder.add(format.actualFormat)
660-
}
657+
override fun dateTimeComponents(format: DateTimeFormat<DateTimeComponents>) =
658+
actualBuilder.add((format as DateTimeComponentsFormat).actualFormat)
661659

662660
override fun createEmpty(): Builder = Builder(AppendableFormatStructure())
663661
}

core/common/src/format/DateTimeFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ internal sealed class AbstractDateTimeFormat<T, U : Copyable<U>> : DateTimeForma
116116

117117
open fun valueFromIntermediateOrNull(intermediate: U): T? = try {
118118
valueFromIntermediate(intermediate)
119-
} catch (e: IllegalArgumentException) {
119+
} catch (_: IllegalArgumentException) {
120120
null
121121
}
122122

core/common/src/format/DateTimeFormatBuilder.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,9 @@ internal fun DateTimeFormatBuilder.WithTime.secondFractionInternal(
362362
maxLength: Int,
363363
grouping: List<Int>
364364
) {
365-
@Suppress("NO_ELSE_IN_WHEN")
366-
when (this) {
367-
is AbstractWithTimeBuilder -> addFormatStructureForTime(
368-
BasicFormatStructure(FractionalSecondDirective(minLength, maxLength, grouping))
369-
)
370-
}
365+
(this as AbstractWithTimeBuilder).addFormatStructureForTime(
366+
BasicFormatStructure(FractionalSecondDirective(minLength, maxLength, grouping))
367+
)
371368
}
372369

373370
/**

core/common/src/format/LocalDateFormat.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,8 @@ internal interface AbstractWithDateBuilder : AbstractWithYearMonthBuilder, DateT
280280
override fun dayOfYear(padding: Padding) =
281281
addFormatStructureForDate(BasicFormatStructure(DayOfYearDirective(padding)))
282282

283-
@Suppress("NO_ELSE_IN_WHEN")
284-
override fun date(format: DateTimeFormat<LocalDate>) = when (format) {
285-
is LocalDateFormat -> addFormatStructureForDate(format.actualFormat)
286-
}
283+
override fun date(format: DateTimeFormat<LocalDate>) =
284+
addFormatStructureForDate((format as LocalDateFormat).actualFormat)
287285
}
288286

289287
// these are constants so that the formats are not recreated every time they are used

core/common/src/format/LocalDateTimeFormat.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ internal interface AbstractWithDateTimeBuilder:
6767
addFormatStructureForDateTime(structure)
6868
}
6969

70-
@Suppress("NO_ELSE_IN_WHEN")
71-
override fun dateTime(format: DateTimeFormat<LocalDateTime>) = when (format) {
72-
is LocalDateTimeFormat -> addFormatStructureForDateTime(format.actualFormat)
73-
}
70+
override fun dateTime(format: DateTimeFormat<LocalDateTime>) =
71+
addFormatStructureForDateTime((format as LocalDateTimeFormat).actualFormat)
7472
}
7573

7674
// these are constants so that the formats are not recreated every time they are used

core/common/src/format/LocalTimeFormat.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ internal interface AbstractWithTimeBuilder : DateTimeFormatBuilder.WithTime {
122122
override fun secondFraction(minLength: Int, maxLength: Int) =
123123
addFormatStructureForTime(BasicFormatStructure(FractionalSecondDirective(minLength, maxLength)))
124124

125-
@Suppress("NO_ELSE_IN_WHEN")
126-
override fun time(format: DateTimeFormat<LocalTime>) = when (format) {
127-
is LocalTimeFormat -> addFormatStructureForTime(format.actualFormat)
128-
}
125+
override fun time(format: DateTimeFormat<LocalTime>) =
126+
addFormatStructureForTime((format as LocalTimeFormat).actualFormat)
129127
}
130128

131129
private class HourDirective(private val padding: Padding) :

core/common/src/format/Unicode.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package kotlinx.datetime.format
77

8-
import kotlin.native.concurrent.*
9-
108
/**
119
* Marks declarations in the datetime library that use format strings to define datetime formats.
1210
*
@@ -241,8 +239,7 @@ internal sealed interface UnicodeFormat {
241239
data class StringLiteral(val literal: String) : UnicodeFormat {
242240
override fun toString(): String = if (literal == "'") "''" else
243241
if (literal.any { it.isLetter() }) "'$literal'"
244-
else if (literal.isEmpty()) ""
245-
else literal
242+
else literal.ifEmpty { "" }
246243
}
247244

248245
sealed class Directive : UnicodeFormat {
@@ -257,8 +254,8 @@ internal sealed interface UnicodeFormat {
257254
sealed class YearMonthBased : DateBased() {
258255
abstract fun addToFormat(builder: DateTimeFormatBuilder.WithYearMonth)
259256
override fun addToFormat(builder: DateTimeFormatBuilder.WithDate) {
260-
val downcastedBuilder: DateTimeFormatBuilder.WithYearMonth = builder
261-
addToFormat(downcastedBuilder)
257+
val downcastBuilder: DateTimeFormatBuilder.WithYearMonth = builder
258+
addToFormat(downcastBuilder)
262259
}
263260

264261
class Era(override val formatLength: Int) : YearMonthBased() {

core/common/src/format/UtcOffsetFormat.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ internal interface AbstractWithOffsetBuilder : DateTimeFormatBuilder.WithUtcOffs
3434
override fun offsetSecondsOfMinute(padding: Padding) =
3535
addFormatStructureForOffset(BasicFormatStructure(UtcOffsetSecondOfMinuteDirective(padding)))
3636

37-
@Suppress("NO_ELSE_IN_WHEN")
38-
override fun offset(format: DateTimeFormat<UtcOffset>) = when (format) {
39-
is UtcOffsetFormat -> addFormatStructureForOffset(format.actualFormat)
40-
}
37+
override fun offset(format: DateTimeFormat<UtcOffset>) =
38+
addFormatStructureForOffset((format as UtcOffsetFormat).actualFormat)
4139
}
4240

4341
internal class UtcOffsetFormat(override val actualFormat: CachedFormatStructure<UtcOffsetFieldContainer>) :

0 commit comments

Comments
 (0)