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
Naming/MemoizedInstanceVariableName false positive #9487
Comments
This def f
@f = other_method1
@f ||= other_method2
end https://docs.rubocop.org/rubocop/1.9/cops_naming.html#namingmemoizedinstancevariablename |
This is not an appropriate change for the example above. In the example above, the assignment to a specific instance variable is the intended side effect. The return value is unused and ignored. Here is a more real life scenario from a Rails controller: def show
@favorite_color = current_user.favorite_color
@favorite_color ||= default_color
# implicitly render "show" template
end In this example, changing the instance variable to |
I would also like to reiterate that the provided examples are not memoizing the function. The instance variables are intentionally recalculated every call. That is why I consider this a false positive. Suggestion: If the method has at least one unconditional assignment, then this offense should not occur. (For example |
I agree. This example isn’t memorization, even though it uses |
Ah, I get it! It seems that my understanding was not enough 💦 |
What happens if you explicitly end your method with Currently you are returning |
I agree that adding But I still think the unconditional assignment should be enough to silence the offense (if possible). While adding an extra |
Noting that I'm experiencing similar behavior that appears to be a false positive.
Obviously in this case, using |
@tenpaiyomi Indeed. Returning |
@marcandre given how the view components work, it is necessitated that I set the options in |
Oh, I see. Isn't it the |
@marcandre yes, in this case it appears that rubocop is incorrectly assuming the I was able to shift the |
Maybe we can reduce the number of false positives by restricting this offense to |
I think the biggest issue in this case is that A few alternatives that would work and don't throw any issues are
or
but neither of those (personally) feel as clean and straightforward as the current solution. The issue you present would definitely help, but in a case of my original code where I was not modifying I'll mull over potentials to try and present, as this is certainly an odd case. |
So guys, what a result of this discussion? Is it still a bug and we have to fix it? Or we can leave it as is? |
There's no clear concensus. Maybe Rails app will simply disable this cop. |
IMO, so long as this cop reports false positives, this should continue to be considered an issue. That is still the case today. If the false positives can't be resolved, then perhaps the next best thing is to mark this cop as unsafe. IIUC, complying with a safe offense should not change outward behavior. |
Oh, right, I thought it was unsafe already. I opened #9487 |
No config
Input file
Expected behavior
No error. The method isn't memoizing a value, it is recalculated every run.
Actual behavior
A workaround to avoid the false positive would be to write the code as:
However, depending on the size and complexity of the expression, that may not be preferred. The assignment to the instance variable is the desirable side effect.
Suggestion: If final
||=
expression had a previous unconditional assignment, then this offense should not occur.The text was updated successfully, but these errors were encountered: