class RuboCop::Cop::Performance::DeletePrefix


str.sub!(/^prefix/, ”)
str.sub(/^prefix/, ”)
str.gsub!(/^prefix/, ”)
str.gsub(/^prefix/, ”)
# bad
@example SafeMultiline: false
str.sub!(/^prefix/, ”)
str.sub(/^prefix/, ”)
str.gsub!(/^prefix/, ”)
str.gsub(/^prefix/, ”)
# good
@example SafeMultiline: true (default)
str.delete_prefix!(‘prefix’)
str.delete_prefix(‘prefix’)
# good
str.sub!(/Aprefix/, ”)
str.sub(/Aprefix/, ”)
str.gsub!(/Aprefix/, ”)
str.gsub(/Aprefix/, ”)
# bad
@example
This cop is unsafe because ‘Pathname` has `sub` but not `delete_prefix`.
@safety
The `delete_prefix(’prefix’)‘ method is faster than `gsub(/Aprefix/, ”)`.
for receiver is multiline string.
`^prefix` is unsafe as it will behave incompatible with `delete_prefix`
This cop has `SafeMultiline` configuration option that `true` by default because
can be replaced by `delete_prefix(’prefix’)‘.
This cop identifies places where `gsub(/Aprefix/, ”)` and `sub(/Aprefix/, ”)`
In Ruby 2.5, `String#delete_prefix` has been added.

def on_send(node)

rubocop:disable Metrics/AbcSize
def on_send(node)
  return unless (receiver, bad_method, regexp_str, replace_string = delete_prefix_candidate?(node))
  return unless replace_string.empty?
  good_method = PREFERRED_METHODS[bad_method]
  message = format(MSG, current: bad_method, prefer: good_method)
  add_offense(node.loc.selector, message: message) do |corrector|
    regexp_str = drop_start_metacharacter(regexp_str)
    regexp_str = interpret_string_escapes(regexp_str)
    string_literal = to_string_literal(regexp_str)
    new_code = "#{receiver.source}#{node.loc.dot.source}#{good_method}(#{string_literal})"
    corrector.replace(node, new_code)
  end
end