class RuboCop::Cop::Style::MapToSet
Looks for uses of ‘map.to_set` or `collect.to_set` that could be written with just `to_set`.
@safety
This cop is unsafe, as it can produce false positives if the receiver is not an `Enumerable`.
@example
# bad something.map { |i| i * 2 }.to_set # good something.to_set { |i| i * 2 } # bad [1, 2, 3].collect { |i| i.to_s }.to_set # good [1, 2, 3].to_set { |i| i.to_s }
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/style/map_to_set.rb, line 41 def on_send(node) return unless (to_set_node, map_node = map_to_set?(node)) message = format(MSG, method: map_node.loc.selector.source) add_offense(map_node.loc.selector, message: message) do |corrector| # If the `to_set` call already has a block, do not autocorrect. next if to_set_node.block_literal? autocorrect(corrector, to_set_node, map_node) end end
Also aliased as: on_csend
Private Instance Methods
Source
# File lib/rubocop/cop/style/map_to_set.rb, line 56 def autocorrect(corrector, to_set, map) removal_range = range_between(to_set.loc.dot.begin_pos, to_set.loc.selector.end_pos) corrector.remove(range_with_surrounding_space(removal_range, side: :left)) corrector.replace(map.loc.selector, 'to_set') end