Sha256: 45ca39ee0f538eb4c00455f61542e31be8c14ea7f214b23b6823c91ab4ee41b6
Contents?: true
Size: 1.98 KB
Versions: 8
Compression:
Stored size: 1.98 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Lint # Certain numeric operations have no impact, being: # Adding or subtracting 0, multiplying or dividing by 1 or raising to the power of 1. # These are probably leftover from debugging, or are mistakes. # # @example # # # bad # x + 0 # x - 0 # x * 1 # x / 1 # x ** 1 # # # good # x # # # bad # x += 0 # x -= 0 # x *= 1 # x /= 1 # x **= 1 # # # good # x = x # class UselessNumericOperation < Base extend AutoCorrector MSG = 'Do not apply inconsequential numeric operations to variables.' RESTRICT_ON_SEND = %i[+ - * / **].freeze # @!method useless_operation?(node) def_node_matcher :useless_operation?, '(send (send nil? $_) $_ (int $_))' # @!method useless_abbreviated_assignment?(node) def_node_matcher :useless_abbreviated_assignment?, '(op-asgn (lvasgn $_) $_ (int $_))' def on_send(node) return unless useless_operation?(node) variable, operation, number = useless_operation?(node) return unless useless?(operation, number) add_offense(node) do |corrector| corrector.replace(node, variable) end end def on_op_asgn(node) return unless useless_abbreviated_assignment?(node) variable, operation, number = useless_abbreviated_assignment?(node) return unless useless?(operation, number) add_offense(node) do |corrector| corrector.replace(node, "#{variable} = #{variable}") end end private def useless?(operation, number) if number.zero? true if %i[+ -].include?(operation) elsif number == 1 true if %i[* / **].include?(operation) end end end end end end
Version data entries
8 entries across 8 versions & 1 rubygems