Sha256: 8abc19a30b54050569d3b92d847d7044919bf3dfa885efe8f5a1f6192341502a

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 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 OnMethodDef

        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_def(_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

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.30.0 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.29.1 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.29.0 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.28.0 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.27.1 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.27.0 lib/rubocop/cop/style/redundant_return.rb