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

UnusedPrivateMember: detect top level declarations #968

Merged
merged 2 commits into from
Jun 27, 2018
Merged

UnusedPrivateMember: detect top level declarations #968

merged 2 commits into from
Jun 27, 2018

Conversation

marianosimone
Copy link
Contributor

This is one approach to allow UnusedPrivateMember to look at top level declarations as well.

Initial discussion in #948

@marianosimone
Copy link
Contributor Author

@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())
Copy link
Contributor Author

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

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

Copy link
Member

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",
Copy link
Contributor Author

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>) {
Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

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.

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())
Copy link
Member

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>) {
Copy link
Member

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) {
Copy link
Contributor Author

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)

Copy link
Member

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>()
Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

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

Okay

@schalkms
Copy link
Member

Is this still work in progress? If not, I’ll approve this PR then.

@marianosimone
Copy link
Contributor Author

@schalkms sorry.. no, I think this PR is good to go. I'll work on adding unused class detection in a separate one

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.

👍

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

Successfully merging this pull request may close these issues.

4 participants