Skip to content

Improve correctness of UnusedPrivateProperty #5935

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

Merged

Conversation

luanpotter
Copy link
Contributor

@luanpotter luanpotter commented Mar 24, 2023

Fixes #5934

Basically ignore package declarations when counting references for the purposes of identifying Unused Private Properties.

Followups

I believe there are many other edge cases that could be considered; for example, the right-hand side of a dot-expression is also being considered a use reference:

class A (val a: String = "foo")

fun main() {
    val a = 1
    println(A().a)
}

Should cause an error, but it doesn't. I'm not sure if there is a better solution here other than exploring each edge case individually, but I am happy to chip down on at least the more obvious ones on followup PRs.

@github-actions github-actions bot added the rules label Mar 24, 2023
super.visitReferenceExpression(expression)
}

private fun skipNode(expression: KtReferenceExpression): Boolean {
return when {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: adding a bit of structure as we can followup with more exceptions and rules for other edge cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

example of a followup: ignore import statements as well

@luanpotter luanpotter force-pushed the luan.unused-private-property-improvements branch from c5983cb to 4385b42 Compare March 24, 2023 15:47
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

detekt found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@codecov
Copy link

codecov bot commented Mar 24, 2023

Codecov Report

Merging #5935 (fbc8a85) into main (39461cf) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##               main    #5935   +/-   ##
=========================================
  Coverage     84.46%   84.46%           
  Complexity     3784     3784           
=========================================
  Files           546      546           
  Lines         12923    12928    +5     
  Branches       2268     2271    +3     
=========================================
+ Hits          10915    10920    +5     
  Misses          877      877           
  Partials       1131     1131           
Impacted Files Coverage Δ
...rbosch/detekt/rules/style/UnusedPrivateProperty.kt 93.33% <100.00%> (+0.60%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

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

Thanks for the quick fix!
Besides the redundant two tests, this PR looks very good!

}

@Test
fun `package declarations are ignored`() {
Copy link
Member

Choose a reason for hiding this comment

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

I do think we only need this test to be added. The other cases are covered by the existing tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point! I had skimmed the file for these tests but initially failed to see them nested in the inner classes.
I removed the other two.

on that note though, lmk of including my 1 test under a new "irrelevant references are ignored" nested class (it seems all tests are nested somewhere)
I was planning on following this up with some other edge cases (eg import statements should be ignored as well)
but I am happy to put the test nested somewhere else if that makes sense, just lmk where it better fits

Copy link
Member

Choose a reason for hiding this comment

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

Regarding test under new nested class 👍
Regarding follow up --> please submit a new PR, it's easier to review 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, def meant followup PRs! thanks for reviewing so swiftly :)

@BraisGabin BraisGabin added this to the 1.23.0 milestone Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UnusedPrivateMember rule counts package declarations as "usage references"
3 participants