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 (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
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
permalink #abbreviated_assignment_with_constant_result?(node) ⇒ Object
[View source]
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 $_lhs) $_operation ({int lvar} $_rhs))' |
permalink #on_op_asgn(node) ⇒ Object
[View source]
69 70 71 72 73 74 75 76 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 69 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 |
permalink #on_send(node) ⇒ Object Also known as: on_csend
[View source]
59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 59 def on_send(node) 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 |
permalink #operation_with_constant_result?(node) ⇒ Object
[View source]
52 53 |
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 52 def_node_matcher :operation_with_constant_result?, '(call (call nil? $_lhs) $_operation ({int | call nil?} $_rhs))' |