Skip to content

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

henni99
Copy link

@henni99 henni99 commented Mar 7, 2025

Summary

try to resolve #344 !

Requirements (place an x in each [ ])

The following point can be removed after setting up CI (such as Travis) with coverage reports (such as Codecov)

  • I've written tests to cover the new code and functionality included in this PR.

The following point can be removed after setting up a CLA reporting tool such as cla-assistant.io

Copy link

salesforce-cla bot commented Mar 7, 2025

Thanks for the contribution! Before we can merge this, we need @henni99 to sign the Salesforce Inc. Contributor License Agreement.

Comment on lines 16 to 33
"""
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
}
}
"""
Copy link
Collaborator

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

Copy link
Author

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.",
Copy link
Collaborator

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

Copy link
Author

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 !🚀

@henni99 henni99 requested a review from ZacSweers March 8, 2025 05:17
@henni99
Copy link
Author

henni99 commented Mar 11, 2025

@ZacSweers I would greatly appreciate your review 😊

Copy link
Collaborator

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other PR, can you tidy up the large indents in your multiline strings?

image

import org.jetbrains.uast.kotlin.isKotlin
import slack.lint.util.sourceImplementation

class NotNullReadOnlyVariableDetector : Detector(), SourceCodeScanner {
Copy link
Collaborator

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

Copy link
Author

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!

Copy link
Author

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'.

@henni99 henni99 changed the title Add NotNullReadOnlyVariableDetector to detect unnecessary null initialization Add AlwaysNullReadOnlyVariableDetector to detect unnecessary null initialization Mar 18, 2025
import com.android.tools.lint.checks.infrastructure.TestMode
import org.junit.Test

class AlwaysNullReadOnlyVariableDetectorTest : BaseSlackLintTest() {
Copy link
Collaborator

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

Copy link
Author

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!

@ZacSweers
Copy link
Collaborator

@henni99 are you planning to address the CI failures?

@henni99
Copy link
Author

henni99 commented Mar 29, 2025

@ZacSweers I plan to proceed with it tomorrow! (complete)

@henni99 henni99 force-pushed the main branch 2 times, most recently from d7599ae to 2704205 Compare March 31, 2025 14:35
@henni99
Copy link
Author

henni99 commented Apr 10, 2025

@ZacSweers Are you planning to review it?

@ZacSweers
Copy link
Collaborator

Sorry I missed a notification that this was updated, I'll try to look today

@henni99
Copy link
Author

henni99 commented May 1, 2025

Any Updates?

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

Successfully merging this pull request may close these issues.

New check: Warning on val class properties assigned to null
2 participants