I expect the following to pass formatting.
package com.zak.test
import com.zak.test.MyClass.Intention.Intention1
import com.zak.test.MyClass.Intention.Intention2
import kotlinx.coroutines.CoroutineScope
class MyClass(
scope: CoroutineScope
) : CoroutineScope by scope {
private val lambda = {
var currentState = State()
for (intention in listOf(Intention1(), Intention2())) {
when (intention) {
is Intention1 -> currentState // import is used here
is Intention2 -> currentState
}
}
}
fun someFunction() {
Intention.Intention1() // notice `Intention.` prefix is used even though it is imported
}
class State
sealed class Intention {
class Intention1 : Intention()
class Intention2 : Intention()
}
}
Fails with: Unused import (no-unused-imports)
If I remove the someFunction(), or define it like so:
fun someFunction() {
Intention1() // import is used, which ktlint likes
}
run ktlint over the above file ^^
0.38.1 (command line)4.0.1 with AGP 3.610.15.6found one more! :)
could be related to #886
Another reproducer:
package com.zak.result
import com.zak.result.Result.Expected
import com.zak.result.Result.Unexpected
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
class ResultTest {
@Test
fun `invoke of any should be expected`() {
assertThat(Result.invoke { 1 }).isEqualTo(Expected(1))
}
@Test
fun `invoke of exception should be unexpected`() {
val ex = Exception()
assertThat(Result.invoke { throw ex }).isEqualTo(Unexpected(ex))
}
@Test
fun `just`() {
assertThat(Result.just(5)).isEqualTo(Result.Expected(5))
}
@Test
fun `raise`() {
val exception = Exception("test")
assertThat(Result.raise(exception)).isEqualTo(Result.Unexpected(exception))
}
}
I think this is a separate from #886 issue as in this case 'no-unused-imports' rule is failing.
Interestingly I could not reproduce it from master build on your first sample, though second one fails.
I think first example has been resolved by https://github.com/pinterest/ktlint/pull/889.
Minimalized second example:
import com.zak.result.Result.Expected
import com.zak.result.Result.Unexpected
import org.assertj.core.api.Assertions.assertThat
fun test() {
assertThat(Result.just(1)).isEqualTo(Expected(1))
assertThat(Result.just(1)).isEqualTo(Result.Expected(1))
val ex = Exception()
assertThat(Result.raise(exception)).isEqualTo(Unexpected(ex))
assertThat(Result.raise(exception)).isEqualTo(Result.Unexpected(exception))
}