Sha256: 587ad2d4e77f5e5da176443a4e35e202ae2b3b6fc4dd6d22f24ed0b17f8b05be

Contents?: true

Size: 1002 Bytes

Versions: 4

Compression:

Stored size: 1002 Bytes

Contents

require 'railroader/checks/base_check'

class Railroader::CheckDivideByZero < Railroader::BaseCheck
  Railroader::Checks.add_optional self

  @description = "Warns on potential division by zero"

  def run_check
    tracker.find_call(:method => :"/").each do |result|
      check_division result
    end
  end

  def check_division result
    return unless original? result

    call = result[:call]

    denominator = call.first_arg

    if number? denominator and denominator.value == 0
      numerator = call.target

      if number? numerator
        if numerator.value.is_a? Float
          return # 0.0 / 0 is NaN and 1.0 / 0 is Infinity
        else
          confidence = :medium
        end
      else
        confidence = :weak
      end

      warn :result => result,
        :warning_type => "Divide by Zero",
        :warning_code => :divide_by_zero,
        :message => "Potential division by zero",
        :confidence => confidence,
        :user_input => denominator
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
railroader-4.3.8 lib/railroader/checks/check_divide_by_zero.rb
railroader-4.3.7 lib/railroader/checks/check_divide_by_zero.rb
railroader-4.3.5 lib/railroader/checks/check_divide_by_zero.rb
railroader-4.3.4 lib/railroader/checks/check_divide_by_zero.rb