lib/rubocop/cop/performance/start_with.rb in rubocop-performance-1.6.0 vs lib/rubocop/cop/performance/start_with.rb in rubocop-performance-1.6.1
- old
+ new
@@ -1,30 +1,47 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
- # This cop identifies unnecessary use of a regex where
- # `String#start_with?` would suffice.
+ # This cop identifies unnecessary use of a regex where `String#start_with?` would suffice.
#
+ # This cop has `SafeMultiline` configuration option that `true` by default because
+ # `^start` is unsafe as it will behave incompatible with `start_with?`
+ # for receiver is multiline string.
+ #
# @example
# # bad
# 'abc'.match?(/\Aab/)
# /\Aab/.match?('abc')
# 'abc' =~ /\Aab/
# /\Aab/ =~ 'abc'
# 'abc'.match(/\Aab/)
# /\Aab/.match('abc')
#
+ # # good
+ # 'abc'.start_with?('ab')
+ #
+ # @example SafeMultiline: true (default)
+ #
+ # # good
# 'abc'.match?(/^ab/)
# /^ab/.match?('abc')
# 'abc' =~ /^ab/
# /^ab/ =~ 'abc'
# 'abc'.match(/^ab/)
# /^ab/.match('abc')
#
- # # good
- # 'abc'.start_with?('ab')
+ # @example SafeMultiline: false
+ #
+ # # bad
+ # 'abc'.match?(/^ab/)
+ # /^ab/.match?('abc')
+ # 'abc' =~ /^ab/
+ # /^ab/ =~ 'abc'
+ # 'abc'.match(/^ab/)
+ # /^ab/.match('abc')
+ #
class StartWith < Cop
include RegexpMetacharacter
MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \
'the beginning of the string.'