-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[analyzer] extend unused_element
for public declarations inside private ones
#48734
Comments
I believe it would be safe to say that it's unused if
But the last case is fairly involved to check and I suspect that the number of cases where we could definitely conclude that the member can't be used is fairly small, so I'm not sure it's worth the effort (either in terms of our time to write the code or in terms of the processor time to run the code for every public member in every private class). |
I thought that this is not possible, because the class is private. But I can see that it can be exposed by class P {
_A get() => _A();
}
class _A {
m() {}
} In this case, we can also check if the the private class is exposed by a public method (in a public declaration). Is there any other way to expose a private class to be used from outside the library?
In fact, this was detected, for a method inside a Flutter widget And in regards the processor time, the following case is already handled, so my guess it wouldn't harm to also add the public methods to the same check (with not so big coding effort, hopefully). class _A {
_m() {}
} |
Note the similarities between this topic and that of dart-lang/language#2020, which is about the ability to override a private member. That issue mentions several different situations where an override may occur, even though it is perhaps tempting to assume that it is not possible. Here is one situation which is taken from that issue and adjusted for this context: Any public subtype The ability to invoke a member like It is possible that we could ignore dynamic invocations, though: We're looking for situations where |
The mentioned issue dives so deep in the language ocean. I understand the burden of the open/closed world analysis. How about the minimal level, in which only the library in question is analyzed, and in which no exposure of the parent class. I would refer to the usual Flutter stateful widget: class My extends StatefulWidget {
State<My> createState() => _MyState();
}
class _MyState extends State<My> {
Widget build(BuildContext context) => Container();
void myPublic() {}
} Of course, any feature that can lead to a potential public access, would not trigger the diagnostic. Later steps could follow, similar to the mentioned topic. |
The algorithm would definitely have to do more work to identify if a private type is exposed publicly. I don't think any of our current analyses do this. Tricky as well (meaning, high potential for bugs, and definite maintenance cost). Case examples:
It's a solvable problem, but I think requires making a reference graph, and a very tricky walk of the graph to find private types which escape. |
Currently, unused_element doesn't consider public declarations which are inside private ones.
For example:
Class
_A
is private, so is not visible outside the defining library.Method
m
is not used (in this example), and can not be used outside.Would it make sense to extend
unused_element
to cover such cases?The text was updated successfully, but these errors were encountered: