-
Notifications
You must be signed in to change notification settings - Fork 170
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
unnecessary_lambdas
false negative
#5056
Comments
Just pinging you since I've changed a bit of the description here @pq. |
Thanks! |
I pointed this out in the discord thread as well, but causing the lint to trigger here could lead to subtle bugs. If the variable is final, tear offs are okay. But since this variable is mutable, it could be changed by later code (even if that code doesn't exist today, it should be able to change without causing unexpected behavior). If the variable is changed after the tear off is used, then the object that the method is called on changes, so there is an observable difference between a tear off and a closure. There is a separate lint to consider making your variable final, which would then safely trigger this lint afterwards. I think it should probably stay that way since being final is the only thing that makes this safe |
If things stay as they are today, you are probably right.
If the above is ever created and active by default or some base set, then this issue should be considered. I can use your logic to argue to simply stop using variable tear-offs today:
So yes, I agree with you. But I still think this is worth fixing. I do agree we could wait for the new lint to be implemented to follow on this thread. EditThis way, the new lint would warn the user that the existing tear-off might not be doing what they thought. Later I'll create the new issue for the proposal and mention this one there and vice-versa. |
Well, the key difference is that making a final variable suddenly mutable and changing it is a deliberate choice that requires thought and consideration that changing the value won't mess up other code. So if someone does that and now the tear-off uses the wrong value, that should be part of the decision when making the variable mutable (an alternative would be to make a copy of the final variable, and make the copy mutable). But yes, if a lint like |
Here it is #5058. Also, thanks for the naming suggestion. |
Maybe we should just stop recommending using tearoffs. That would mean just not having |
Hey! A little update, with final variables the lint is triggering now. Also, I found this issue wich is somewhat the opposite, similar to what I asked on #5058. |
Describe the issue
If I have a Widget state similar to:
I see no indication that I can use a tear-off like
c.toggle
. In cases where the variable isfinal
it does trigger.Originally I thought that class members could also be false-negatives but after a discussion on Discord (with @abitofevrything), I saw that if the class is extended and the field is replaced for a getter, then it can change and that would be a true negative.
Here is the old description.
If I have a Widget state similar to:
I see no indication that I can use a tear-off like
animationController.toggle
. In cases where the variable isfinal
/const
or is a local variable where we can prove that it hasn't changed, this is a viable trigger.To Reproduce
Sample above.
Expected behavior
unnecessary_lambdas
should trigger for unchanging variables.Additional context
Maybe there could also be a lint to warn users off of using tear-off whenever the variable might change. WDYT?
The text was updated successfully, but these errors were encountered: