diff --git a/lib/rubocop/cop/style/when_then.rb b/lib/rubocop/cop/style/when_then.rb index c790e275328..5febd0a1da3 100644 --- a/lib/rubocop/cop/style/when_then.rb +++ b/lib/rubocop/cop/style/when_then.rb @@ -20,12 +20,16 @@ module Style class WhenThen < Base extend AutoCorrector - MSG = 'Do not use `when x;`. Use `when x then` instead.' + MSG = 'Do not use `when %s;`. Use `when %s then` instead.' def on_when(node) return if node.multiline? || node.then? || !node.body - add_offense(node.loc.begin) { |corrector| corrector.replace(node.loc.begin, ' then') } + message = format(MSG, expression: node.conditions.map(&:source).join(', ')) + + add_offense(node.loc.begin, message: message) do |corrector| + corrector.replace(node.loc.begin, ' then') + end end end end diff --git a/spec/rubocop/cop/style/when_then_spec.rb b/spec/rubocop/cop/style/when_then_spec.rb index 81819699114..0e119533cc9 100644 --- a/spec/rubocop/cop/style/when_then_spec.rb +++ b/spec/rubocop/cop/style/when_then_spec.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Style::WhenThen, :config do - it 'registers an offense for when x;' do + it 'registers an offense for when b;' do expect_offense(<<~RUBY) case a when b; c - ^ Do not use `when x;`. Use `when x then` instead. + ^ Do not use `when b;`. Use `when b then` instead. end RUBY @@ -16,6 +16,21 @@ RUBY end + it 'registers an offense for when b, c;' do + expect_offense(<<~RUBY) + case a + when b, c; d + ^ Do not use `when b, c;`. Use `when b, c then` instead. + end + RUBY + + expect_correction(<<~RUBY) + case a + when b, c then d + end + RUBY + end + it 'accepts ; separating statements in the body of when' do expect_no_offenses(<<~RUBY) case a