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

Don't hide nullability problems behind a cast #3484

Closed
BraisGabin opened this issue Feb 19, 2021 · 4 comments · Fixed by #5653
Closed

Don't hide nullability problems behind a cast #3484

BraisGabin opened this issue Feb 19, 2021 · 4 comments · Fixed by #5653
Assignees

Comments

@BraisGabin
Copy link
Member

Expected Behavior of the rule

Noncompliant:

fun foo(bar: Any?) {
  val x = bar as String
}

Compliant:

fun foo(bar: Any?) {
  val x = bar!! as String
}

Context

The cast operator can hide null problems in your code. I propose a rule that forbid any cast from a nullable type to a non-null type. If you want to do that you first should cast from nullable to non-null and then do the cast as shown in the example.

The idea is to write code that show that it could fails for two things (nullability and type) and not just one (cast).

I had this idea reading #3479 so they are related but they are not the same.

@github-actions
Copy link

github-actions bot commented Nov 6, 2021

This issue is stale because it has been open 90 days with no activity. Please comment or this will be closed in 7 days.

@atulgpt
Copy link
Contributor

atulgpt commented Dec 29, 2022

Hi @BraisGabin I am willing to implement this issue, should I proceed and will your review the PR?

@BraisGabin
Copy link
Member Author

Please, go ahead! I'll review it.

@atulgpt
Copy link
Contributor

atulgpt commented Dec 31, 2022

Hi @BraisGabin added my fix at #5653. This PR might contain some issues as this is my first time working with Kotlin PSI and detekt. Thanks for reviewing this PR

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

Successfully merging a pull request may close this issue.

3 participants