Skip to content

Commit

Permalink
Merge pull request #10590 from nobuyo/fix-autocorrect-for-style-raise…
Browse files Browse the repository at this point in the history
…-args-with-compact-style

[Fix #10589] Fix autocorrect for `Style/RaiseArgs` with `EnforcedStyle: compact`
  • Loading branch information
koic committed Apr 29, 2022
2 parents 72d6299 + 4883af4 commit b2edf74
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_autocorrect_for_styleraiseargs.md
@@ -0,0 +1 @@
* [#10589](https://github.com/rubocop/rubocop/issues/10589): Fix autocorrect for `Style/RaiseArgs` with `EnforcedStyle: compact` and exception object is assigned to a local variable. ([@nobuyo][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/raise_args.rb
Expand Up @@ -80,7 +80,7 @@ def correction_exploded_to_compact(node)
return node.source if message_nodes.size > 1

argument = message_nodes.first.source
exception_class = exception_node.const_name || exception_node.receiver.source
exception_class = exception_node.receiver&.source || exception_node.source

if node.parent && requires_parens?(node.parent)
"#{node.method_name}(#{exception_class}.new(#{argument}))"
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/raise_args_spec.rb
Expand Up @@ -17,6 +17,19 @@
end
end

context 'with a raise with 2 args and exception object is assigned to a local variable' do
it 'reports an offense' do
expect_offense(<<~RUBY)
raise error_class, msg
^^^^^^^^^^^^^^^^^^^^^^ Provide an exception object as an argument to `raise`.
RUBY

expect_correction(<<~RUBY)
raise error_class.new(msg)
RUBY
end
end

context 'with a raise with exception instantiation and message arguments' do
it 'reports an offense' do
expect_offense(<<~RUBY)
Expand Down Expand Up @@ -317,6 +330,10 @@ def do_something
expect_no_offenses('raise RuntimeError, msg')
end

it 'accepts a raise when exception object is assigned to a local variable' do
expect_no_offenses('raise error_class, msg')
end

it 'accepts a raise with msg argument' do
expect_no_offenses('raise msg')
end
Expand Down

0 comments on commit b2edf74

Please sign in to comment.