Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

UnsafeCallOnNullableType not reporting !! usage #2036

Closed
lyrafelipe opened this issue Oct 18, 2019 · 10 comments
Closed

UnsafeCallOnNullableType not reporting !! usage #2036

lyrafelipe opened this issue Oct 18, 2019 · 10 comments

Comments

@lyrafelipe
Copy link

lyrafelipe commented Oct 18, 2019

Expected Behavior

UnsafeCallOnNullableType rule should report usages of !! as an unsafe call on nullable type.

Observed Behavior

UnsafeCallOnNullableType rule isn't reporting !! as an unsafe call on nullable type after 1.1.0 update.

Steps to Reproduce

When running the following test:

class UnsafeCallOnNullableTypeTest {
    private val code: String =
            """
                val aString: String?
                aString = null
                val aStringLength = aString!!.length
            """.trimIndent()

    @Test
    fun shouldReportDoubleBangUsage() {
        val findings = UnsafeCallOnNullableType().lint(code)

        assertThat(findings).hasSize(1)
        assertThat(findings[0].message).isEqualTo("Calling !! on a nullable type will throw a " +
                "NullPointerException at runtime in case the value is null. It should be avoided.")
    }
}

The test won't pass if the UnsafeCallOnNullableType rule after f80fe60 commit is used but, if you remove the 3 lines added to the rule by the linked commit or use version 1.0.1, it will pass.

Your Environment

  • Version of detekt used: 1.1.1
  • Version of Gradle used (if applicable): 5.6.2
  • Operating System and version: Ubuntu Mate 18.04
  • Link to your project (if it's a public repository): Private Repository
@3flex
Copy link
Member

3flex commented Oct 18, 2019

The behaviour of the rule has changed to now require type resolution, which means if you're running detekt without type resolution (not passing a classpath) then it won't run the rule.

We need docs for this and have an issue open already to cover that.

If you're actually running detekt with type resolution and the rule is still not picking up this case then we'll need to look more closely at the problem.

@lyrafelipe
Copy link
Author

By passing a classpath you mean running detekt with --classpath flag? If yes, could you link an example?

@3flex
Copy link
Member

3flex commented Oct 18, 2019

One way would be running gradlew detektMain with debug output and looking for the flags passed to detekt when it runs.

@lyrafelipe
Copy link
Author

lyrafelipe commented Oct 18, 2019

I ran the task gradlew detekt, since I didn't have the task detektMain, with debug output and couldn't find any classpath flag being passed to detekt.

Could you please clarify how should I do this? Is there a way to declare it inside my detekt block like toolVersion, input, filters, etc?

@3flex
Copy link
Member

3flex commented Oct 19, 2019

Gradle creates tasks for each source set but currently only on Kotlin JVM projects, is it maybe an Android, native or JS project?

We definitely need docs for this so probably creating those is the action we need to take to resolve this, see #1943

@lyrafelipe
Copy link
Author

lyrafelipe commented Oct 19, 2019

It's an Android project.

If there's something else I can help, please let me know.

@3flex
Copy link
Member

3flex commented Oct 19, 2019

Yeah we're not generating required Gradle tasks for Android yet. It's on my to do list; I'll work on it next since there's a clear need.

@schalkms
Copy link
Member

schalkms commented Aug 3, 2020

The required Gradle tasks for Android are described as a todo in another issue. Therefore, I’ll close this.

@schalkms schalkms closed this as completed Aug 3, 2020
@lyrafelipe
Copy link
Author

@schalkms Can you link the issue with this TODO?

@schalkms
Copy link
Member

schalkms commented Aug 4, 2020

Link #2259

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants