-
Notifications
You must be signed in to change notification settings - Fork 16
Add AlwaysNullReadOnlyVariableDetector to detect unnecessary null initialization #358
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for the contribution! Before we can merge this, we need @henni99 to sign the Salesforce Inc. Contributor License Agreement. |
""" | ||
package foo | ||
|
||
val nullableKtString: String? = null | ||
|
||
class Test { | ||
val nullableString: String? = null | ||
val nullableFloat: Float? = null | ||
|
||
fun method() { | ||
val nullableLocalString: String? = null | ||
} | ||
|
||
companion object { | ||
val nullableCompanionString: String? = null | ||
} | ||
} | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's clean up this huge leading indent. Same with the expect block below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I will try it !🚀
Issue.create( | ||
"AvoidNullInitializationForReadOnlyVariables", | ||
"Avoid initializing read-only variable with null", | ||
"Avoid unnecessary null initialization for read-only variables.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This explanation should be a bit more longer. i.e. explaining why it's bad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I will try it !🚀
@ZacSweers I would greatly appreciate your review 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import org.jetbrains.uast.kotlin.isKotlin | ||
import slack.lint.util.sourceImplementation | ||
|
||
class NotNullReadOnlyVariableDetector : Detector(), SourceCodeScanner { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name seems wrong. By definition the variable is nullable, the issue is that we want to point out when a nullable type can only ever be null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your great review! I think the naming needs some refinement, so I'll take more time to consider it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest the naming 'AlwaysNullReadOnlyVariableDetector'.
import com.android.tools.lint.checks.infrastructure.TestMode | ||
import org.junit.Test | ||
|
||
class AlwaysNullReadOnlyVariableDetectorTest : BaseSlackLintTest() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some cases that should not warn to these tests? i.e. mutable variables
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added test code.
While exploring more cases, I thought it would be good to consider scenarios where a read-only property's custom getter returns null, so I have made adjustments accordingly.
I would appreciate your feedback.
Thank you!
@henni99 are you planning to address the CI failures? |
@ZacSweers I plan to proceed with it tomorrow! (complete) |
d7599ae
to
2704205
Compare
@ZacSweers Are you planning to review it? |
Sorry I missed a notification that this was updated, I'll try to look today |
Any Updates? |
Summary
try to resolve #344 !
Requirements (place an
x
in each[ ]
)-> happen 404 error