Class: RuboCop::Cop::Style::FloatDivision
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle
- Defined in:
- lib/rubocop/cop/style/float_division.rb
Overview
Checks for division with integers coerced to floats. It is recommended to either always use ‘fdiv` or coerce one side only. This cop also provides other options for code consistency.
For ‘Regexp.last_match` and nth reference (e.g., `$1`), it assumes that the value is a string matched by a regular expression, and allows conversion with `#to_f`.
Constant Summary collapse
- MESSAGES =
{ left_coerce: 'Prefer using `.to_f` on the left side.', right_coerce: 'Prefer using `.to_f` on the right side.', single_coerce: 'Prefer using `.to_f` on one side only.', fdiv: 'Prefer using `fdiv` for float divisions.' }.freeze
- RESTRICT_ON_SEND =
%i[/].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #any_coerce?(node) ⇒ Object
- #both_coerce?(node) ⇒ Object
- #left_coerce?(node) ⇒ Object
- #on_send(node) ⇒ Object
- #regexp_last_match?(node) ⇒ Object
- #right_coerce?(node) ⇒ Object
- #to_f_method?(node) ⇒ Object
Methods included from AutoCorrector
Methods included from ConfigurableEnforcedStyle
#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#any_coerce?(node) ⇒ Object
82 83 84 |
# File 'lib/rubocop/cop/style/float_division.rb', line 82 def_node_matcher :any_coerce?, <<~PATTERN {(send _ :/ #to_f_method?) (send #to_f_method? :/ _)} PATTERN |
#both_coerce?(node) ⇒ Object
78 79 80 |
# File 'lib/rubocop/cop/style/float_division.rb', line 78 def_node_matcher :both_coerce?, <<~PATTERN (send #to_f_method? :/ #to_f_method?) PATTERN |
#left_coerce?(node) ⇒ Object
74 75 76 |
# File 'lib/rubocop/cop/style/float_division.rb', line 74 def_node_matcher :left_coerce?, <<~PATTERN (send #to_f_method? :/ _) PATTERN |
#on_send(node) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rubocop/cop/style/float_division.rb', line 98 def on_send(node) return unless offense_condition?(node) add_offense(node) do |corrector| case style when :left_coerce, :single_coerce add_to_f_method(corrector, node.receiver) remove_to_f_method(corrector, node.first_argument) when :right_coerce remove_to_f_method(corrector, node.receiver) add_to_f_method(corrector, node.first_argument) when :fdiv correct_from_slash_to_fdiv(corrector, node, node.receiver, node.first_argument) end end end |
#regexp_last_match?(node) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/rubocop/cop/style/float_division.rb', line 91 def_node_matcher :regexp_last_match?, <<~PATTERN { (send (const {nil? cbase} :Regexp) :last_match int) (:nth_ref _) } PATTERN |
#right_coerce?(node) ⇒ Object
70 71 72 |
# File 'lib/rubocop/cop/style/float_division.rb', line 70 def_node_matcher :right_coerce?, <<~PATTERN (send _ :/ #to_f_method?) PATTERN |
#to_f_method?(node) ⇒ Object
86 87 88 |
# File 'lib/rubocop/cop/style/float_division.rb', line 86 def_node_matcher :to_f_method?, <<~PATTERN (send !nil? :to_f) PATTERN |