Class: RuboCop::Cop::DevDoc::Style::LiteralOrInWhenClause
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::DevDoc::Style::LiteralOrInWhenClause
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/dev_doc/style/literal_or_in_when_clause.rb
Overview
Flag || / && between literals inside a when clause. The operator
evaluates immediately (:a || :b is just :a), so the other literal
silently never matches — when alternatives must be comma-separated.
Rationale
when :issuing_country || :nationality reads like "either of these"
but compiles to when :issuing_country; the :nationality branch is
dead from day one and nothing at runtime ever hints at it. No core or
rubocop-rails cop catches this shape. Restricted to literal operands
the pattern is provably wrong, so the false-positive surface is
effectively zero (when SOME_CONST || fallback and variable operands
are left alone — unusual, but potentially intentional).
❌ :nationality can never match
when :issuing_country || :nationality
✔️ comma-separated alternatives
when :issuing_country, :nationality
Autocorrect rewrites || chains to a comma list. It is marked unsafe
because it deliberately changes behavior: the dead alternative starts
matching, which is the fix — but any code relying on the buggy
behavior must be re-verified. Chains containing nil/false and all
&& forms are flagged without autocorrect (the intent is ambiguous).
Constant Summary collapse
- MSG =
'`%<source>s` evaluates immediately to `%<result>s`, so the other ' \ 'literal(s) in it can never match this `when` clause. List ' \ 'alternatives with commas instead: `when %<list>s`.'.freeze
- LITERAL_TYPES =
%i[sym str int float true false nil].freeze
- FALSY_TYPES =
%i[false nil].freeze
Instance Method Summary collapse
Instance Method Details
#on_case(node) ⇒ Object
52 53 54 55 56 |
# File 'lib/rubocop/cop/dev_doc/style/literal_or_in_when_clause.rb', line 52 def on_case(node) node.when_branches.each do |branch| branch.conditions.each { |condition| check(condition) } end end |