Skip to content
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

Prefer include? over member? in Style/CollectionMethods #7793

Merged
merged 1 commit into from Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### New features

* [#7793](https://github.com/rubocop-hq/rubocop/pull/7793): Prefer `include?` over `member?` in `Style/CollectionMethods`. ([@dmolesUC][])
* [#7654](https://github.com/rubocop-hq/rubocop/issues/7654): Support `with_fixed_indentation` option for `Layout/ArrayAlignment` cop. ([@nikitasakov][])

### Bug fixes
Expand Down Expand Up @@ -4389,3 +4390,4 @@
[@djudd]: https://github.com/djudd
[@jemmaissroff]: https://github.com/jemmaissroff
[@nikitasakov]: https://github.com/nikitasakov
[@dmolesUC]: https://github.com/dmolesUC
3 changes: 2 additions & 1 deletion config/default.yml
Expand Up @@ -2455,7 +2455,7 @@ Style/ClassVars:
# Align with the style guide.
Style/CollectionMethods:
Description: 'Preferred collection methods.'
StyleGuide: '#map-find-select-reduce-size'
StyleGuide: '#map-find-select-reduce-include-size'
Enabled: false
VersionAdded: '0.9'
VersionChanged: '0.27'
Expand All @@ -2472,6 +2472,7 @@ Style/CollectionMethods:
inject: 'reduce'
detect: 'find'
find_all: 'select'
member?: 'include?'

Style/ColonMethodCall:
Description: 'Do not use :: for method call.'
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/collection_methods.rb
Expand Up @@ -27,13 +27,15 @@ module Style
# items.inject
# items.detect
# items.find_all
# items.member?
#
# # good
# items.map
# items.map!
# items.reduce
# items.find
# items.select
# items.include?
#
class CollectionMethods < Cop
include MethodPreference
Expand Down
6 changes: 4 additions & 2 deletions manual/cops_style.md
Expand Up @@ -752,24 +752,26 @@ items.collect!
items.inject
items.detect
items.find_all
items.member?

# good
items.map
items.map!
items.reduce
items.find
items.select
items.include?
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
PreferredMethods | `{"collect"=>"map", "collect!"=>"map!", "inject"=>"reduce", "detect"=>"find", "find_all"=>"select"}` |
PreferredMethods | `{"collect"=>"map", "collect!"=>"map!", "inject"=>"reduce", "detect"=>"find", "find_all"=>"select", "member?"=>"include?"}` |

### References

* [https://rubystyle.guide#map-find-select-reduce-size](https://rubystyle.guide#map-find-select-reduce-size)
* [https://rubystyle.guide#map-find-select-reduce-include-size](https://rubystyle.guide#map-find-select-reduce-include-size)

## Style/ColonMethodCall

Expand Down
3 changes: 2 additions & 1 deletion spec/rubocop/cop/style/collection_methods_spec.rb
Expand Up @@ -6,7 +6,8 @@
'collect' => 'map',
'inject' => 'reduce',
'detect' => 'find',
'find_all' => 'select'
'find_all' => 'select',
'member?' => 'include?'
}
}

Expand Down