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: Nullable val can be non-nullable #4376

Closed
BraisGabin opened this issue Dec 16, 2021 · 2 comments
Closed

New rule: Nullable val can be non-nullable #4376

BraisGabin opened this issue Dec 16, 2021 · 2 comments
Labels

Comments

@BraisGabin
Copy link
Member

Expected Behavior of the rule

Detect variables and properties that are declared as Nullable but they never contain a null value.

Non-compilant:

val a: String? = "hello"

val a: String?
   get() = "hello"

Context

The tanslator of code from java to kotlin adds a lot of unnecesary ?. And when you are refactoring maybe changes a property that could hold null to something that couldn't hold null but you can forget about redeclaring the type.

I don't know if we could handle var too.

After reading the last commits of #4353 I relasied that this rule shouldn't be that difficult to implement.

@severn-everett
Copy link
Contributor

severn-everett commented Dec 16, 2021

I think the case where var could be flagged is if the analyzer could prove that any values that it gets assigned are non-null (or at least not nullable). Private vars - along with vars that have private setters - seen pretty feasible for this; vars that are settable outside of their containing object/class might balloon the complexity (and/or computational time) quite a bit. In any case, I'll take a look.

@cortinico
Copy link
Member

Closing this as it was shipped in #4379

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

No branches or pull requests

3 participants