Sha256: 299950515dff3d56b7176e49be31f49042357b91064007fbb518d74bdb6fd094

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 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
        MSG = 'Redundant `return` detected.'

        def on_def(node)
          _method_name, _args, body = *node

          check(body)
        end

        def on_defs(node)
          _scope, _method_name, _args, body = *node

          check(body)
        end

        private

        def autocorrect_action(node)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.expression,
                              node.loc.expression.source.sub('return ', ''))
          end
        end

        def check(node)
          return unless node

          if node.type == :return
            convention(node, :keyword)
          elsif node.type == :begin
            expressions = *node
            last_expr = expressions.last

            if last_expr && last_expr.type == :return
              convention(last_expr, :keyword)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.13.1 lib/rubocop/cop/style/redundant_return.rb
rubocop-0.13.0 lib/rubocop/cop/style/redundant_return.rb