-
-
Notifications
You must be signed in to change notification settings - Fork 774
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
UnusedPrivateMember: detect top level declarations #968
Conversation
@schalkms this still doesn't handle detecting private classes, but wanted to get initial feedback before going down that path. It ended up being less complicated than my initial implementation, mainly because all the detection was already in place, and just needed to change the conditions under which they happen (note that there are no new visitors or anything) |
@@ -103,17 +107,14 @@ class UnusedPrivateMember(config: Config = Config.empty) : Rule(config) { | |||
} | |||
|
|||
override fun visitProperty(property: KtProperty) { | |||
if ((property.isPrivate() && property.isNonNestedMember()) | |||
if ((property.isPrivate() && property.isMemberOrTopLevel()) |
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 did some testing around this, and didn't really get why it was trying to exclude nonNestedMember
s.
I added some tests for the new functionality.
Note that, before this, something like:
class A {
class B {
private val silentlyIgnored = 1
}
}
wouldn't be detected
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 think @Mauin could help out here.
@@ -177,7 +177,7 @@ class UnusedPrivateMemberSpec : SubjectSpek<UnusedPrivateMember>({ | |||
val code = """ | |||
fun RuleSetProvider.provided() = ruleSetId in defaultRuleSetIds | |||
|
|||
private val defaultRuleSetIds = listOf("comments", "complexity", "empty-blocks", |
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.
had to remove the private, as now the rule would complain about it
@@ -80,6 +80,6 @@ val stuff = object : Iterator<String?> { | |||
override fun hasNext(): Boolean = true | |||
} | |||
|
|||
fun main(args: Array<String>) { |
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.
had to make this one ignored, or it would be detected
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.
We might want to ignore the main method declaration.
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.
Done!
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.
Looks fine!
@@ -103,17 +107,14 @@ class UnusedPrivateMember(config: Config = Config.empty) : Rule(config) { | |||
} | |||
|
|||
override fun visitProperty(property: KtProperty) { | |||
if ((property.isPrivate() && property.isNonNestedMember()) | |||
if ((property.isPrivate() && property.isMemberOrTopLevel()) |
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 think @Mauin could help out here.
@@ -80,6 +80,6 @@ val stuff = object : Iterator<String?> { | |||
override fun hasNext(): Boolean = true | |||
} | |||
|
|||
fun main(args: Array<String>) { |
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.
We might want to ignore the main method declaration.
@@ -95,25 +96,33 @@ class UnusedPrivateMember(config: Config = Config.empty) : Rule(config) { | |||
} | |||
} | |||
|
|||
override fun visitPrimaryConstructor(constructor: KtPrimaryConstructor) { |
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.
Added this functionality (and think it's one of the most useful ones!.. me and my team usually do a bad job at removing no-longer used parameters from constructors)
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.
Definitely! I like this feature.
|
||
private val properties = mutableMapOf<String, KtElement>() | ||
private val properties = mutableSetOf<KtNamedDeclaration>() |
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.
Using a map here no longer works, as the visitor could be looking at clashing names in different scopes. We can just use a set of the declarations, filter it before returning it, and just report from there
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
Is this still work in progress? If not, I’ll approve this PR then. |
@schalkms sorry.. no, I think this PR is good to go. I'll work on adding unused class detection in a separate one |
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 is one approach to allow
UnusedPrivateMember
to look at top level declarations as well.Initial discussion in #948