Sha256: 3e1876227214d1ad0e83fc825168a5bc1616938263a8af12ad0292e200518c05

Contents?: true

Size: 997 Bytes

Versions: 2

Compression:

Stored size: 997 Bytes

Contents

require 'roodi/checks/check'

module Roodi
  module Checks
    # Checks the body of a rescue block to make sure it's not empty..
    # 
    # When the body of a rescue block is empty, exceptions can get caught and swallowed without
    # any feedback to the user.
    class EmptyRescueBodyCheck < Check
      STATEMENT_NODES = [:fcall, :return, :attrasgn, :vcall, :nil]
      
      def interesting_nodes
        [:resbody]
      end

      def evaluate(node)
        add_error("Rescue block should not be empty.", 1) unless has_statement?(node)
      end
  
      private
  
      def has_statement?(node)
        return true if STATEMENT_NODES.include?(node.node_type)
        return true if assigning_other_than_exception_to_local_variable?(node) 
        return true if node.children.any? { |child| has_statement?(child) }
      end

      def assigning_other_than_exception_to_local_variable?(node)
        node.node_type == :lasgn && node[2].to_a != [:gvar, :$!]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roodi-1.3.7 lib/roodi/checks/empty_rescue_body_check.rb
roodi-1.3.6 lib/roodi/checks/empty_rescue_body_check.rb