Sha256: 286d65055f96b7899f623a644e7c9a9ebba93c1c1d5e23771e573b88854464c2

Contents?: true

Size: 1.01 KB

Versions: 45

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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

45 entries across 41 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/top_level_return_with_argument.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/top_level_return_with_argument.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.50.2 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.50.1 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.50.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.49.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
call_your_name-0.1.0 vendor/bundle/ruby/3.1.0/gems/rubocop-1.48.1/lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.48.1 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.48.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.47.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/lint/top_level_return_with_argument.rb
rubocop-1.46.0 lib/rubocop/cop/lint/top_level_return_with_argument.rb