diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ea3c066b88..c2db9b40d8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) @@ -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 diff --git a/lib/rubocop/cop/style/raise_args.rb b/lib/rubocop/cop/style/raise_args.rb index a4352dbcb41..429980b1b40 100644 --- a/lib/rubocop/cop/style/raise_args.rb +++ b/lib/rubocop/cop/style/raise_args.rb @@ -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) @@ -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) diff --git a/spec/rubocop/cop/style/raise_args_spec.rb b/spec/rubocop/cop/style/raise_args_spec.rb index f189880c5da..8bd43447e60 100644 --- a/spec/rubocop/cop/style/raise_args_spec.rb +++ b/spec/rubocop/cop/style/raise_args_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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