Skip to content

Commit

Permalink
Merge pull request #8957 from marcandre/comp_doc
Browse files Browse the repository at this point in the history
Improve doc for MultipleComparison
  • Loading branch information
koic committed Oct 28, 2020
2 parents 5e03bf5 + 7ba5874 commit 49d8ccd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -6074,7 +6074,8 @@ end
|===

This cop checks against comparing a variable with multiple items, where
`Array#include?` could be used instead to avoid code repetition.
`Array#include?`, `Set#include?` or a `case` could be used instead
to avoid code repetition.
It accepts comparisons of multiple method calls to avoid unnecessary method calls
by default. It can be configured by `AllowMethodComparison` option.

Expand All @@ -6089,6 +6090,17 @@ foo if a == 'a' || a == 'b' || a == 'c'
# good
a = 'a'
foo if ['a', 'b', 'c'].include?(a)
VALUES = Set['a', 'b', 'c'].freeze
# elsewhere...
foo if VALUES.include?(a)
case foo
when 'a', 'b', 'c' then foo
# ...
end
# accepted (but consider `case` as above)
foo if a == b.lightweight || a == b.heavyweight
----

Expand Down
14 changes: 13 additions & 1 deletion lib/rubocop/cop/style/multiple_comparison.rb
Expand Up @@ -4,7 +4,8 @@ module RuboCop
module Cop
module Style
# This cop checks against comparing a variable with multiple items, where
# `Array#include?` could be used instead to avoid code repetition.
# `Array#include?`, `Set#include?` or a `case` could be used instead
# to avoid code repetition.
# It accepts comparisons of multiple method calls to avoid unnecessary method calls
# by default. It can be configured by `AllowMethodComparison` option.
#
Expand All @@ -16,6 +17,17 @@ module Style
# # good
# a = 'a'
# foo if ['a', 'b', 'c'].include?(a)
#
# VALUES = Set['a', 'b', 'c'].freeze
# # elsewhere...
# foo if VALUES.include?(a)
#
# case foo
# when 'a', 'b', 'c' then foo
# # ...
# end
#
# # accepted (but consider `case` as above)
# foo if a == b.lightweight || a == b.heavyweight
#
# @example AllowMethodComparison: true (default)
Expand Down

0 comments on commit 49d8ccd

Please sign in to comment.