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
Detect useless assignment and return #7373
Comments
I will try to make it |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding! |
This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it. |
😞 |
@tjwallace I don't particularly like stale bot either. You are free to reopen this. Are you sure this is a pattern that happens enough to warrant a check? Note that there can not be any code between |
Can confirm this is a popular pattern - I have seen those many times. Also I have found 3 offenses in |
Sorry, upgrading late here... This cop might be a bit aggressive. We have tons of data manipulation code like this: def normalize(input)
string = input
string = string.gsub(...)
string = string.split.first
string = string.upcase
string
end Stylistically there are many ways to approach this, but removing the final assignment is probably not helpful. Thanks for the new rule, we would be happy to enable if it was slightly less aggressive. |
Maybe I'm taking your example too literally, but isn't it all much easier to understand without the def normalize(input)
input
.gsub(...)
.split.first
.upcase
end |
Sometimes we use method chaining, but often we avoid it. I probably should've been more literal with my example. This is still made up, but more realistic. This pattern makes it easy to document, reorder, etc. Quite common with data processing IMO. def normalize(input)
string = input
# clean up whitespace
string = string.gsub(...)
# grab the first word
string = string.split.first
# normalize case to use as hash key
string = string.upcase
string
end |
Inline comments work well too, reordering is not an issue either... 😅 def normalize(input)
input
.string.gsub(...) # clean up whitespace
.split.first # grab the first word
.upcase # normalize case to use as hash key
end |
Given this code, I'd like RuboCop to detect that the assignment is useless and autocorrect the code to remove the assignment.
I'm not sure if this is a new cop or an addition to the existing
Link/UselessAssignment
cop.The text was updated successfully, but these errors were encountered: