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
#config, #processed_source, #project_index
Instance Method Summary collapse
- #abbreviated_assignment_with_constant_result?(node) ⇒ Object
- #on_op_asgn(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
- #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
cop_dir_for, #exclude_limit, read_limits
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
64 65 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 64 def_node_matcher :abbreviated_assignment_with_constant_result?, '(op-asgn (lvasgn $_lhs) $_operation ({int lvar} $_rhs))' |
#on_op_asgn(node) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 80 def on_op_asgn(node) return unless (lhs, operation, rhs = abbreviated_assignment_with_constant_result?(node)) return unless (result = constant_result?(lhs, operation, rhs)) add_offense(node) do |corrector| corrector.replace(node, "#{lhs} = #{result}") end end |
#on_send(node) ⇒ Object Also known as: on_csend
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 67 def on_send(node) # Safe navigation short-circuits to `nil` when the receiver is `nil`, so the # result is not constant and replacing it with `0`/`1` would change behavior. return if node.csend_type? return unless (lhs, operation, rhs = operation_with_constant_result?(node)) return unless (result = constant_result?(lhs, operation, rhs)) add_offense(node) do |corrector| corrector.replace(node, result.to_s) end end |
#operation_with_constant_result?(node) ⇒ Object
60 61 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 60 def_node_matcher :operation_with_constant_result?, '(call (call nil? $_lhs) $_operation ({int | call nil?} $_rhs))' |