Sha256: a62e02ea6680f7f1256cc746196795d05d9dfa09f4a650655ee26749dd8de09e

Contents?: true

Size: 1.32 KB

Versions: 150

Compression:

Stored size: 1.32 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
      #   # bad
      #   return 1
      #
      #   # good
      #   return
      class TopLevelReturnWithArgument < Base
        extend AutoCorrector

        MSG = 'Top level return with argument detected.'

        def on_return(return_node)
          return unless top_level_return_with_any_argument?(return_node)

          add_offense(return_node) do |corrector|
            remove_arguments(corrector, return_node)
          end
        end

        private

        def top_level_return_with_any_argument?(return_node)
          top_level_return?(return_node) && return_node.arguments?
        end

        def remove_arguments(corrector, return_node)
          corrector.replace(return_node, 'return')
        end

        # 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.
        def top_level_return?(return_node)
          return_node.each_ancestor(:block, :def, :defs).none?
        end
      end
    end
  end
end

Version data entries

150 entries across 149 versions & 15 rubygems

Version Path
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/top_level_return_with_argument.rb