Sha256: 5dcdcd54d3dafa1bcabcb4fcc6f9ba4ada506b032081ece55b926f74890dca2d

Contents?: true

Size: 1.02 KB

Versions: 73

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for top level return with arguments. If there is a
      # top-level return statement with an argument, then the argument is
      # always ignored. This is detected automatically since Ruby 2.7.
      #
      # @example
      #
      #   # Detected since Ruby 2.7
      #   return 1 # 1 is always ignored.
      class TopLevelReturnWithArgument < Base
        # This cop works by validating the ancestors of the return node. A
        # top-level return node's ancestors should not be of block, def, or
        # defs type.

        MSG = 'Top level return with argument detected.'

        def on_return(return_node)
          add_offense(return_node) if return_node.arguments? && ancestors_valid?(return_node)
        end

        private

        def ancestors_valid?(return_node)
          prohibited_ancestors = return_node.each_ancestor(:block, :def, :defs)
          prohibited_ancestors.none?
        end
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 8 rubygems

Version Path
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.23.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.22.3 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.22.2 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.22.1 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.22.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.21.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.20.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.19.1 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.19.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.18.4 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.18.3 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.18.2 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.18.1 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.18.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.17.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.16.1 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.16.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb