Ktlint: Unused import issue with 38.1

Created on 11 Sep 2020  路  4Comments  路  Source: pinterest/ktlint

Expected Behavior

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

Observed Behavior

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
}

Steps to Reproduce

run ktlint over the above file ^^

Your Environment

  • Version of ktlint used: 0.38.1 (command line)
  • AS 4.0.1 with AGP 3.6
  • Operating System and version: MacOS 10.15.6
bug

All 4 comments

found 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))
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xenomachina picture xenomachina  路  5Comments

zsavely picture zsavely  路  5Comments

Jacksgong picture Jacksgong  路  3Comments

impatient picture impatient  路  5Comments

Tapchicoma picture Tapchicoma  路  3Comments