Skip to content

Commit 2bf71f1

Browse files
authored
Merge pull request #14418 from koic/fix_false_positives_for_style_map_to_set
Fix false positives for `Style/MapToSet`
2 parents f807efa + d7b9750 commit 2bf71f1

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14418](https://github.com/rubocop/rubocop/pull/14418): Fix false positives for `Style/MapToSet` when using `to_set` with block argument. ([@koic][])

lib/rubocop/cop/style/map_to_set.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ class MapToSet < Base
4040

4141
def on_send(node)
4242
return unless (to_set_node, map_node = map_to_set?(node))
43+
return if to_set_node.block_literal?
4344

4445
message = format(MSG, method: map_node.loc.selector.source)
4546
add_offense(map_node.loc.selector, message: message) do |corrector|
46-
# If the `to_set` call already has a block, do not autocorrect.
47-
next if to_set_node.block_literal?
48-
4947
autocorrect(corrector, to_set_node, map_node)
5048
end
5149
end

spec/rubocop/cop/style/map_to_set_spec.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,27 @@
116116
end
117117
end
118118

119-
context "`#{method}.to_set` with a block on `to_set`" do
120-
it 'registers an offense but does not correct' do
121-
expect_offense(<<~RUBY, method: method)
119+
context "`#{method}` followed by `to_set` with a block passed to `to_set`" do
120+
it 'does not register an offense but does not correct' do
121+
expect_no_offenses(<<~RUBY)
122+
foo.#{method} { |x| x * 2 }.to_set { |x| [x.to_s, x] }
123+
RUBY
124+
end
125+
end
126+
127+
context "`#{method}` followed by `to_set` with a numbered block passed to `to_set`", :ruby27 do
128+
it 'does not register an offense but does not correct' do
129+
expect_no_offenses(<<~RUBY)
122130
foo.#{method} { |x| x * 2 }.to_set { |x| [x.to_s, x] }
123-
^{method} Pass a block to `to_set` instead of calling `#{method}.to_set`.
124131
RUBY
132+
end
133+
end
125134

126-
expect_no_corrections
135+
context "`#{method}` followed by `to_set` with an `it` block passed to `to_set`", :ruby34 do
136+
it 'does not register an offense but does not correct' do
137+
expect_no_offenses(<<~RUBY)
138+
foo.#{method} { |x| x * 2 }.to_set { |x| [x.to_s, x] }
139+
RUBY
127140
end
128141
end
129142

0 commit comments

Comments
 (0)