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

New rule: toString over a nullable value #2901

Closed
BraisGabin opened this issue Jul 27, 2020 · 0 comments · Fixed by #2903
Closed

New rule: toString over a nullable value #2901

BraisGabin opened this issue Jul 27, 2020 · 0 comments · Fixed by #2903

Comments

@BraisGabin
Copy link
Member

BraisGabin commented Jul 27, 2020

Expected Behavior of the rule

val a: Any?
val key: String = a.toString() // this line should be flagged
val key2: String = "$a" // same here

This code compiles, and have a defined behaviour. But, usually, it is a potential bug. The idea is to have a rule that detects the use of toString() (explicit or implicit) and flag it.

Context

It's really strange that you really want to print "null" when you have a null value. If you are working with UI you probably want "", "-" or something else. And the same works for any other piece of code for example this one: https://github.com/detekt/detekt/pull/2898/files#diff-6576486bfd0ee34b1da2a5967fba6cf4R27

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