Class: RuboCop::Cop::Performance::Casecmp
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Performance::Casecmp
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/performance/casecmp.rb
Overview
Identifies places where a case-insensitive string comparison can better be implemented using ‘casecmp`.
This cop is disabled by default because ‘String#casecmp` only works with ASCII characters. See github.com/rubocop/rubocop/issues/9753.
If you are working only with ASCII characters, then this cop can be safely enabled.
Constant Summary collapse
- MSG =
'Use `%<good>s` instead of `%<bad>s`.'
- RESTRICT_ON_SEND =
%i[== eql? !=].freeze
- CASE_METHODS =
%i[downcase upcase].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubocop/cop/performance/casecmp.rb', line 58 def on_send(node) return unless downcase_eq(node) || eq_downcase(node) return unless (parts = take_method_apart(node)) _receiver, method, arg, variable = parts good_method = build_good_method(method, arg, variable) = format(MSG, good: good_method, bad: node.source) add_offense(node, message: ) do |corrector| autocorrect(corrector, node, good_method) end end |