Class: RuboCop::Cop::Lint::NumericOperationWithConstantResult
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb
Overview
Certain numeric operations have a constant result, usually 0 or 1. Multiplying a number by 0 will always return 0. Dividing a number by itself or raising it to the power of 0 will always return 1. As such, they can be replaced with that result. These are probably leftover from debugging, or are mistakes. Other numeric operations that are similarly leftover from debugging or mistakes are handled by Lint/UselessNumericOperation.
NOTE: This cop doesn’t detect offenses for the ‘-` and `%` operator because it can’t determine the type of ‘x`. If `x` is an Array or String, it doesn’t perform a numeric operation.
Constant Summary collapse
- MSG =
'Numeric operation with a constant result detected.'
- RESTRICT_ON_SEND =
%i[* / **].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #abbreviated_assignment_with_constant_result?(node) ⇒ Object
- #on_op_asgn(node) ⇒ Object
- #on_send(node) ⇒ Object
- #operation_with_constant_result?(node) ⇒ Object
Methods included from AutoCorrector
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?, #message, #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
#abbreviated_assignment_with_constant_result?(node) ⇒ Object
56 57 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 56 def_node_matcher :abbreviated_assignment_with_constant_result?, '(op-asgn (lvasgn $_) $_ ({int | lvar} $_))' |
#on_op_asgn(node) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 71 def on_op_asgn(node) return unless abbreviated_assignment_with_constant_result?(node) variable, operation, number = abbreviated_assignment_with_constant_result?(node) result = constant_result?(variable, operation, number) return unless result add_offense(node) do |corrector| corrector.replace(node, "#{variable} = #{result}") end end |
#on_send(node) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 59 def on_send(node) return unless operation_with_constant_result?(node) variable, operation, number = operation_with_constant_result?(node) result = constant_result?(variable, operation, number) return unless result add_offense(node) do |corrector| corrector.replace(node, result.to_s) end end |
#operation_with_constant_result?(node) ⇒ Object
52 53 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 52 def_node_matcher :operation_with_constant_result?, '(send (send nil? $_) $_ ({int | send nil?} $_))' |