Skip to content

Commit 8d2ef48

Browse files
Earlopainbbatsov
authored andcommitted
Fix an infinite loop for Style/RaiseArgs with EnforcedStyle: compact when passing than 2 arguments to raise
The following test caused the loop: https://github.com/rubocop/rubocop/blob/5ee786dcadfa3c91096d9e81dcd16e4776ca6186/spec/rubocop/cop/style/raise_args_spec.rb#L130-L140 This changes `expect_no_corrections` to always raise if there are correctors. In this case, the corrector replaced nothing, resulting in the same cop being invocted again for the same reason. The source assertion is left in because it produces nice diffs if there are actually changes.
1 parent bb0dd97 commit 8d2ef48

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#12774](https://github.com/rubocop/rubocop/pull/12774): Fix an infinite loop for `Style/RaiseArgs` with `EnforcedStyle: compact` when passing more than 2 arguments to `raise`. ([@earlopain][])

lib/rubocop/cop/style/raise_args.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def correction_compact_to_exploded(node)
8080

8181
def correction_exploded_to_compact(node)
8282
exception_node, *message_nodes = *node.arguments
83-
return node.source if message_nodes.size > 1
83+
return if message_nodes.size > 1
8484

8585
argument = message_nodes.first.source
8686
exception_class = exception_node.receiver&.source || exception_node.source

lib/rubocop/rspec/expect_offense.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,13 @@ def expect_no_corrections
169169

170170
return if @last_corrector.empty?
171171

172-
# In order to print a nice diff, e.g. what source got corrected to,
173-
# we need to run the actual corrections
174-
172+
# This is just here for a pretty diff if the source actually got changed
175173
new_source = @last_corrector.rewrite
176-
177174
expect(new_source).to eq(@processed_source.buffer.source)
175+
176+
# There is an infinite loop if a corrector is present that did not make
177+
# any changes. It will cause the same offense/correction on the next loop.
178+
raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [@offenses])
178179
end
179180

180181
def expect_no_offenses(source, file = nil)

0 commit comments

Comments
 (0)