-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add new Style/ArrayFirstLast
cop
#12416
Conversation
If the methods are specific only to arrays, I guess we can name the cop "ArrayFirstLast" or something more generic like "ArrayElementAccess". Might be good to make this configurable for whoever prefers indices as well, although I don't feel strongly about this.
Makes sense. |
a318917
to
ae7835a
Compare
Style/FirstLast
copStyle/ArrayFirstLast
cop
Changed the name. |
Documenting the reason for being disabled by default will help users understand. |
ae7835a
to
b09b40d
Compare
Added a line regarding why it is disabled. |
# The cop is disabled by default due to safety concerns. | ||
# | ||
# @safety | ||
# Autocorrection is unsafe because `[0]` or `[-1]` can be called on a Hash, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not just the autocorrection is unsafe - the cop is fundamentally unsafe. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, copy paste error 🤦 😅
b09b40d
to
4525396
Compare
Fixes #12406.
I already implemented this cop (so that's why still decided to open a PR) when realised that
.first
/.last
is only suited for arrays. While[0]
and[-1]
can be also used with strings or hashes. So this can probably generate many false positives. I made it disabled by default.