From e9ad8ba3e0b6e1520dded2009d01736279b94c51 Mon Sep 17 00:00:00 2001 From: David Moles Date: Wed, 11 Mar 2020 09:10:53 -0700 Subject: [PATCH] Prefer `include?` over `member?` in `Style/CollectionMethods` --- CHANGELOG.md | 2 ++ config/default.yml | 3 ++- lib/rubocop/cop/style/collection_methods.rb | 2 ++ manual/cops_style.md | 6 ++++-- spec/rubocop/cop/style/collection_methods_spec.rb | 3 ++- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 015c67d126e..99e6d5dce74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -4389,3 +4390,4 @@ [@djudd]: https://github.com/djudd [@jemmaissroff]: https://github.com/jemmaissroff [@nikitasakov]: https://github.com/nikitasakov +[@dmolesUC]: https://github.com/dmolesUC diff --git a/config/default.yml b/config/default.yml index 5049f96a154..bd975358785 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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' @@ -2472,6 +2472,7 @@ Style/CollectionMethods: inject: 'reduce' detect: 'find' find_all: 'select' + member?: 'include?' Style/ColonMethodCall: Description: 'Do not use :: for method call.' diff --git a/lib/rubocop/cop/style/collection_methods.rb b/lib/rubocop/cop/style/collection_methods.rb index 79614e77530..9d7235489ec 100644 --- a/lib/rubocop/cop/style/collection_methods.rb +++ b/lib/rubocop/cop/style/collection_methods.rb @@ -27,6 +27,7 @@ module Style # items.inject # items.detect # items.find_all + # items.member? # # # good # items.map @@ -34,6 +35,7 @@ module Style # items.reduce # items.find # items.select + # items.include? # class CollectionMethods < Cop include MethodPreference diff --git a/manual/cops_style.md b/manual/cops_style.md index 74d1c4459a1..2a33cf7411e 100644 --- a/manual/cops_style.md +++ b/manual/cops_style.md @@ -752,6 +752,7 @@ items.collect! items.inject items.detect items.find_all +items.member? # good items.map @@ -759,17 +760,18 @@ 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 diff --git a/spec/rubocop/cop/style/collection_methods_spec.rb b/spec/rubocop/cop/style/collection_methods_spec.rb index 079cff9099e..f4406eee143 100644 --- a/spec/rubocop/cop/style/collection_methods_spec.rb +++ b/spec/rubocop/cop/style/collection_methods_spec.rb @@ -6,7 +6,8 @@ 'collect' => 'map', 'inject' => 'reduce', 'detect' => 'find', - 'find_all' => 'select' + 'find_all' => 'select', + 'member?' => 'include?' } }