Detect unused private members in classes
See original GitHub issueWhat rule do you want to change?
no-unused-vars
(arguably this could be a separate rule as well, something like no-unused-private-fields
)
Does this change cause the rule to produce more or fewer warnings?
More
How will the change be implemented? (New option, new default behavior, etc.)?
New default behavior
Please provide some example code that this change will affect:
class MyClass {
#unusedField = 5;
#usedFieldOnlyInWrite = 42;
#usedWhenReadingAndWriting = 500;
someMethod() {
this.#usedFieldOnlyInWrite = 42;
this.#usedWhenReadingAndWriting += 1;
}
someOtherMethod() {
return this.#usedWhenReadingAndWriting;
}
}
What does the rule currently do for this code?
Nothing
What will the rule do after it’s changed?
Report unused fields declared in a class. In the above example, it should report that both #unusedField
and #usedFieldOnlyInWrite
are unused. This should be possible, as private fields are only concerned in a particular class context, so you can collect all fields in a class, iterate through all usages and mark does that are never read as unused.
Are you willing to submit a pull request to implement this change?
I would be able to give it a go, but I am not sure how difficult this would be.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:10 (10 by maintainers)
Top GitHub Comments
Makes sense to add this rule. If we want to include private methods in the same rule (I don’t see a reason why we shouldn’t), a name like
no-unused-private-class-members
might be more appropriate.I included this rule in DevTools today and it found a whole bunch of unused fields 🎉 https://crrev.com/c/3310610 I didn’t encounter any further issues nor false positives, so I think we are good here 😄