Class: RuboCop::Cop::Lint::NumericOperationWithConstantResult

Inherits:
Base
  • Object
show all
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.

Examples:


# bad
x * 0

# good
0

# bad
x *= 0

# good
x = 0

# bad
x / x
x ** 0

# good
1

# bad
x /= x
x **= 0

# good
x = 1

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

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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

#exclude_limit

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

silence_warnings

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 $_lhs) $_operation ({int lvar} $_rhs))'

#on_op_asgn(node) ⇒ Object



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

#on_send(node) ⇒ Object Also known as: on_csend



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

#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?,
'(call (call nil? $_lhs) $_operation ({int | call nil?} $_rhs))'
OSZAR »