Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unused import issue with 38.1 #902

Closed
ZakTaccardi opened this issue Sep 10, 2020 · 4 comments · Fixed by #905
Closed

Unused import issue with 38.1 #902

ZakTaccardi opened this issue Sep 10, 2020 · 4 comments · Fixed by #905
Labels
Milestone

Comments

@ZakTaccardi
Copy link

ZakTaccardi commented Sep 10, 2020

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
@ZakTaccardi
Copy link
Author

ZakTaccardi commented Sep 10, 2020

found one more! :)

could be related to #886

@ZakTaccardi
Copy link
Author

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

@Tapchicoma
Copy link
Collaborator

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.

@Tapchicoma Tapchicoma added the bug label Sep 11, 2020
@Tapchicoma Tapchicoma added this to the 0.40.0 milestone Sep 11, 2020
@t-kameyama
Copy link
Contributor

I think first example has been resolved by #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))
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
3 participants