Class: RuboCop::Cop::Performance::RedundantStringChars
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Performance::RedundantStringChars
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/performance/redundant_string_chars.rb
Overview
Checks for redundant ‘String#chars`.
Constant Summary collapse
- MSG =
'Use `%<good_method>s` instead of `%<bad_method>s`.'
- RESTRICT_ON_SEND =
%i[[] slice first last take length size empty?].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 54 def on_send(node) return unless (receiver, method, args = redundant_chars_call?(node)) return if method == :last && !args.empty? range = offense_range(receiver, node) = (method, args) add_offense(range, message: ) do |corrector| range = correction_range(receiver, node) replacement = build_good_method(method, args) corrector.replace(range, replacement) end end |