Sha256: a159784999748b3bbb16933cd2d5e8190f61c70af22591c4d53198393e8eae93

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for redundant `return` expressions.
      #
      # @example
      #
      #   def test
      #     return something
      #   end
      #
      #   def test
      #     one
      #     two
      #     three
      #     return something
      #   end
      #
      # It should be extended to handle methods whose body is if/else
      # or a case expression with a default branch.
      class RedundantReturn < Cop
        include OnMethod

        MSG = 'Redundant `return` detected.'

        private

        def autocorrect(node)
          @corrections << lambda do |corrector|
            if node.children.size > 1
              kids = node.children.map { |child| child.loc.expression }
              corrector.insert_before(kids.first, '[')
              corrector.insert_after(kids.last, ']')
            end
            return_kw = range_with_surrounding_space(node.loc.keyword, :right)
            corrector.remove(return_kw)
          end
        end

        def on_method(_node, _method_name, _args, body)
          return unless body

          if body.type == :return
            check_return_node(body)
          elsif body.type == :begin
            expressions = *body
            last_expr = expressions.last

            if last_expr && last_expr.type == :return
              check_return_node(last_expr)
            end
          end
        end

        def check_return_node(node)
          return if cop_config['AllowMultipleReturnValues'] &&
            node.children.size > 1

          add_offense(node, :keyword)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/redundant_return.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/redundant_return.rb
rubocop-0.26.1 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.26.0 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.25.0 lib/rubocop/cop/style/redundant_return.rb