• Tan Le's avatar
    Add cop to disallow delegating predicate methods · 150e877f
    Tan Le authored
    This cop looks for delegations to predicate methods with `allow_nil`
    option. When delegating with this option enabled, we ends up with true,
    false and nil and this does not preserve the boolean nature of predicate
    methods.
    
    The preferred implementation is to define a method to handle `nil` and
    returns the correct Boolean value instead.
    
    delegate :is_foo?, to: :bar, allow_nil: true
    
    def is_foo?
      return false unless bar
      bar.is_foo?
    end
    
    def is_foo?
      !!bar&.is_foo?
    end
    150e877f
delegate_predicate_methods.rb 1.5 KB