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. Subtracting a number from itself or multiplying it by 0 will always return 0. Additionally, a variable modulo 0 or itself 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.

Examples:


# bad
x - x
x * 0
x % 1
x % x

# good
0

# bad
x -= x
x *= 0
x %= 1
x %= x

# 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_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



59
60
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 59

def_node_matcher :abbreviated_assignment_with_constant_result?,
'(op-asgn (lvasgn $_) $_ ({int | lvar} $_))'

#on_op_asgn(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 74

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



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 62

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



55
56
# File 'lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb', line 55

def_node_matcher :operation_with_constant_result?,
'(send (send nil? $_) $_ ({int | send nil?} $_))'