Skip to content

Commit

Permalink
Merge pull request #8810 from pbernays/fix-style-raise-args
Browse files Browse the repository at this point in the history
[Fix #8759] Fix multiple offense detection for Style/RaiseArgs
  • Loading branch information
koic committed Sep 30, 2020
2 parents c0b8da9 + 40542a7 commit 3fd227f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@

* [#8796](https://github.com/rubocop-hq/rubocop/pull/8796): Add new `Lint/HashCompareByIdentity` cop. ([@fatkodima][])

### Bug fixes

* [#8810](https://github.com/rubocop-hq/rubocop/pull/8810): Fix multiple offense detection for Style/RaiseArgs. ([@pbernays][])

### Changes

* [#8646](https://github.com/rubocop-hq/rubocop/issues/8646): Faster find of all files in `TargetFinder` class which improves rubocop initial startup speed. ([@tleish][])
Expand Down Expand Up @@ -4932,3 +4936,4 @@
[@iSarCasm]: https://github.com/iSarCasm
[@em-gazelle]: https://github.com/em-gazelle
[@tleish]: https://github.com/tleish
[@pbernays]: https://github.com/pbernays
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/raise_args.rb
Expand Up @@ -84,8 +84,6 @@ def correction_exploded_to_compact(node)

def check_compact(node)
if node.arguments.size > 1
return unless opposite_style_detected

add_offense(node, message: format(COMPACT_MSG, method: node.method_name)) do |corrector|
replacement = correction_exploded_to_compact(node)

Expand All @@ -103,7 +101,6 @@ def check_exploded(node)

return unless first_arg.send_type? && first_arg.method?(:new)
return if acceptable_exploded_args?(first_arg.arguments)
return unless opposite_style_detected

add_offense(node, message: format(EXPLODED_MSG, method: node.method_name)) do |corrector|
replacement = correction_compact_to_exploded(node)
Expand Down
53 changes: 48 additions & 5 deletions spec/rubocop/cop/style/raise_args_spec.rb
Expand Up @@ -75,6 +75,30 @@
end
RUBY
end

it 'reports multiple offenses' do
expect_offense(<<~RUBY)
if a
raise RuntimeError, msg
^^^^^^^^^^^^^^^^^^^^^^^ Provide an exception object as an argument to `raise`.
elsif b
raise Ex.new(msg)
else
raise ArgumentError, msg
^^^^^^^^^^^^^^^^^^^^^^^^ Provide an exception object as an argument to `raise`.
end
RUBY

expect_correction(<<~RUBY)
if a
raise RuntimeError.new(msg)
elsif b
raise Ex.new(msg)
else
raise ArgumentError.new(msg)
end
RUBY
end
end

context 'with a raise with 3 args' do
Expand Down Expand Up @@ -107,8 +131,6 @@
raise Ex.new(msg)
^^^^^^^^^^^^^^^^^ Provide an exception class and message as arguments to `raise`.
RUBY
expect(cop.config_to_allow_offenses)
.to eq('EnforcedStyle' => 'compact')

expect_correction(<<~RUBY)
raise Ex, msg
Expand All @@ -122,8 +144,6 @@
raise Ex.new
^^^^^^^^^^^^ Provide an exception class and message as arguments to `raise`.
RUBY
expect(cop.config_to_allow_offenses)
.to eq('EnforcedStyle' => 'compact')

expect_correction(<<~RUBY)
raise Ex
Expand Down Expand Up @@ -181,7 +201,6 @@
^^^^^^^^^^^^^^^^^ Provide an exception class and message as arguments to `raise`.
end
RUBY
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)

expect_correction(<<~RUBY)
if a
Expand All @@ -191,6 +210,30 @@
end
RUBY
end

it 'reports multiple offenses' do
expect_offense(<<~RUBY)
if a
raise RuntimeError, msg
elsif b
raise Ex.new(msg)
^^^^^^^^^^^^^^^^^ Provide an exception class and message as arguments to `raise`.
else
raise ArgumentError.new(msg)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Provide an exception class and message as arguments to `raise`.
end
RUBY

expect_correction(<<~RUBY)
if a
raise RuntimeError, msg
elsif b
raise Ex, msg
else
raise ArgumentError, msg
end
RUBY
end
end

context 'when an exception object is assigned to a local variable' do
Expand Down

0 comments on commit 3fd227f

Please sign in to comment.